AOP(Aspect-Oriented Programming)는 OOP를 보완하는 수단으로, 흩어진 Aspect를 모듈화 할 수 잇는 프로그래밍 기법이다. 즉, 여러곳에서 쓰이는 공통기능을 모듈롸하고, 쓰이는 곳에 필요할 때 연결함으로써, 유지 보수 혹은 재사용에 요이하도록 프로그래밍 하는것.
스프링 AOP
프록시 기반의 AOP 구현체 스프링 빈에만 AOP를 적용할 수 있다. 동적 프록시 빈을 만들어 등록시켜준다.
출처: AOP
ApiException.java
@Getter
@AllArgsConstructor
public class ApiException {
private final String message;
private final HttpStatus httpStatus;
}
ApiRequestException.java
public class ApiRequestException extends IllegalArgumentException {
public ApiRequestException(String message) {
super(message);
}
public ApiRequestException(String message, Throwable cause) {
super(message, sause);
}
}
ApiExceptionHandler.java
@RestControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(value = {ApiRequestException.class})
public ResponseEntity<Object> handleApiRequestException(ApiRequestException ex) {
ApiException apiException = new ApiException(ex.getMessage, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(apiException, HttpStatus.BAD_REQUEST);
}
}