2023.12.17 - [웹/Spring vue 웹 개발] - spring vue local db-> rds 로 변경(h2dabase->postgreSql)
spring vue local db-> rds 로 변경(h2dabase->postgreSql)
2023.12.07 - [웹/Spring vue 웹 개발] - spring vue swagger 변경작업 완료 spring vue swagger 변경작업 완료 2023.12.03 - [웹/Spring vue 웹 개발] - spring vue swagger 변경 작업01 spring vue swagger 변경 작업01 2023.11.27 - [웹/Spri
kwaksh2319.tistory.com
인터럽트:
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
public class GlobalHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
log.info("preHandle [{}][{}]", requestURI, handler);
return true; //false 진행X
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String requestURI = request.getRequestURI();
log.info("postHandle [{}][{}]", requestURI, handler);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String requestURI = request.getRequestURI();
log.info("afterCompletion requestURI [{}][{}]", requestURI, handler);
if (ex != null) {
log.error("afterCompletion error!!", ex);
}
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
.....
//추가
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new GlobalHandlerInterceptor());
}
}
결과:
예외처리:
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class ErrorResult {
private String code;
private String message;
}
public class UserException extends RuntimeException{
public UserException() {
super();
}
public UserException(String message) {
super(message);
}
public UserException(String message, Throwable cause) {
super(message, cause);
}
public UserException(Throwable cause) {
super(cause);
}
protected UserException(String message, Throwable cause, boolean
enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
public ErrorResult illegalExHandle(IllegalArgumentException e) {
log.error("[exceptionHandle] ex", e);
return new ErrorResult("BAD", e.getMessage());
}
@ExceptionHandler
public ResponseEntity<ErrorResult> userExHandle(UserException e) {
log.error("[exceptionHandle] ex", e);
ErrorResult errorResult = new ErrorResult("USER-EX", e.getMessage());
return new ResponseEntity<>(errorResult, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler
public ErrorResult exHandle(Exception e) {
log.error("[exceptionHandle] ex", e);
return new ErrorResult("EX", "내부 오류");
}
}
결과:
'웹 > Spring vue 웹 개발' 카테고리의 다른 글
JWT(json web token) 개발하며 이해하기 (0) | 2024.01.06 |
---|---|
Docker 필요성, 로컬 환경 (윈도우 10), aws e2c(리눅스) 연결 배포 후 실행 (1) | 2023.12.26 |
spring vue local db-> rds 로 변경(h2dabase->postgreSql) (0) | 2023.12.17 |
인텔리제이,spring boot, RDS 연결 (postgreSQL) (0) | 2023.12.16 |
RDS 생성 및 EC2 연결 ( h2database -> postgresql rds ) (0) | 2023.12.16 |