Loading...
Spring Framework Reference Documentation 7.0.2의 Controller Advice의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
See equivalent in the Reactive stack
@ExceptionHandler, @InitBinder, 그리고 @ModelAttribute 메서드는 그것들이 선언된 @Controller 클래스 또는 클래스 계층 구조에만 적용됩니다. 대신, 그것들이 @ControllerAdvice 또는 @RestControllerAdvice 클래스에 선언되면, 어떤 컨트롤러에도 적용됩니다. 더욱이, 5.3부터는 @ControllerAdvice 안의 @ExceptionHandler 메서드를 어떤 @Controller 또는 다른 핸들러로부터의 예외를 처리하는 데 사용할 수 있습니다.
@ControllerAdvice는 @Component로 메타-어노테이션되어 있으며, 따라서 컴포넌트 스캐닝을 통해 Spring 빈으로 등록될 수 있습니다.
@RestControllerAdvice는 @ControllerAdvice와 @ResponseBody를 결합한 쇼트컷 어노테이션으로, 사실상 예외 핸들러 메서드가 응답 본문에 렌더링되는 @ControllerAdvice일 뿐입니다.
시작 시점에, RequestMappingHandlerMapping과 ExceptionHandlerExceptionResolver는 컨트롤러 어드바이스 빈을 감지하고 런타임에 그것들을 적용합니다. @ControllerAdvice의 전역 @ExceptionHandler 메서드는 @Controller의 로컬 것들 이후에 적용됩니다. 반대로, 전역 @ModelAttribute와 @InitBinder 메서드는 로컬 것들 이전에 적용됩니다.
기본적으로, @ControllerAdvice와 @RestControllerAdvice는 @Controller와 @RestController를 포함한 어떤 컨트롤러에도 적용됩니다. 어노테이션의 속성을 사용하여 그것들이 적용되는 컨트롤러와 핸들러의 집합을 좁힐 수 있습니다. 예를 들어:
1// Target all Controllers annotated with @RestController 2@ControllerAdvice(annotations = RestController.class) 3public class ExampleAdvice1 {} 4 5// Target all Controllers within specific packages 6@ControllerAdvice("org.example.controllers") 7public class ExampleAdvice2 {} 8 9// Target all Controllers assignable to specific classes 10@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class}) 11public class ExampleAdvice3 {}
1// Target all Controllers annotated with @RestController 2@ControllerAdvice(annotations = [RestController::class]) 3class ExampleAdvice1 4 5// Target all Controllers within specific packages 6@ControllerAdvice("org.example.controllers") 7class ExampleAdvice2 8 9// Target all Controllers assignable to specific classes 10@ControllerAdvice(assignableTypes = [ControllerInterface::class, AbstractController::class]) 11class ExampleAdvice3
앞선 예제의 셀렉터는 런타임에 평가되며 광범위하게 사용될 경우 성능에 부정적인 영향을 줄 수 있습니다. 더 자세한 내용은
@ControllerAdvice
javadoc을 참고하십시오.
Exceptions
Functional Endpoints