emotional developer/detect-Web
[spring] @ControllerAdvice 여러개 쓰기.
성게군.
2015. 7. 6. 15:42
@ControllerAdvice 를 통해서 error 처리가 가능하다.
두개 이상의 @ControllerAdvice 가 있을 경우, 처리 순서를 지정해야 하는 경우가 있다.
이럴 경우. @Order 어노테이션을 이용해서 우선순위를 지정한다.
http://stackoverflow.com/questions/19498378/setting-precedence-of-multiple-controlleradvice-exceptionhandlers
class UserProfileException extends RuntimeException {
}
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
class UserProfileExceptionHandler {
@ExceptionHandler(UserProfileException)
@ResponseBody
ResponseEntity<ErrorResponse> handleUserProfileException() {
....
}
}
@ControllerAdvice
@Order(Ordered.LOWEST_PRECEDENCE)
class DefaultExceptionHandler {
@ExceptionHandler(RuntimeException)
@ResponseBody
ResponseEntity<ErrorResponse> handleRuntimeException() {
....
}
}
반응형