컨트롤러에서 AOP를 사용하는 경우가 있다면 가장 흔하게 볼 수 있는 것이 예외처리이다. 예외처리를 편리하게 해주기 위하여 사용할 수 있는 어노테이션이 @RestControllerAdvcie 이다.
간단하게 모든 예외에 대한 처리 방법을 만들 경우 아래와 같이 만들 수 있다.
@RestControllerAdvice
public class ExceptionAdvice {
// 모든 예외에 대한 처리
@ExceptionHandler(Exception.class)
public String exceptionAdvice(Exception e) {
return "Exception!!!!!!!!!!!!";
}
}
@RestControllerAdvice(annotations = Example.class)
public class ExceptionAdvice {
}
@ExceptionHandler(value = {NullPointerException.class, ArrayIndexOutOfBoundsException.class})
public String exampleExceptionAdvice(RuntimeException e) {
return "exampleException Occurred!!!!!!!!!!!!!";
}