|
|
|
|
@ -4021,27 +4021,25 @@ necessary methods, and declare it as a Spring bean.
@@ -4021,27 +4021,25 @@ necessary methods, and declare it as a Spring bean.
|
|
|
|
|
=== Controller Advice |
|
|
|
|
[.small]#<<web-reactive.adoc#webflux-ann-controller-advice, WebFlux>># |
|
|
|
|
|
|
|
|
|
Typically `@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply within |
|
|
|
|
the `@Controller` class (or class hierarchy) in which they are declared. If you want such |
|
|
|
|
methods to apply more globally (across controllers), you can declare them in a class |
|
|
|
|
annotated with `@ControllerAdvice` or `@RestControllerAdvice`. |
|
|
|
|
|
|
|
|
|
`@ControllerAdvice` is annotated with `@Component`, which means such classes can be |
|
|
|
|
registered as Spring beans through <<core.adoc#beans-java-instantiating-container-scan, |
|
|
|
|
component scanning>>. `@RestControllerAdvice` is a composed annotation that is annotated |
|
|
|
|
with both `@ControllerAdvice` and `@ResponseBody`, which essentially means |
|
|
|
|
`@ExceptionHandler` methods are rendered to the response body through message conversion |
|
|
|
|
(versus view resolution or template rendering). |
|
|
|
|
|
|
|
|
|
On startup, the infrastructure classes for `@RequestMapping` and `@ExceptionHandler` |
|
|
|
|
methods detect Spring beans annotated with `@ControllerAdvice` and then apply their |
|
|
|
|
methods at runtime. Global `@ExceptionHandler` methods (from a `@ControllerAdvice`) are |
|
|
|
|
applied _after_ local ones (from the `@Controller`). By contrast, global `@ModelAttribute` |
|
|
|
|
and `@InitBinder` methods are applied _before_ local ones. |
|
|
|
|
|
|
|
|
|
By default, `@ControllerAdvice` methods apply to every request (that is, all controllers), |
|
|
|
|
but you can narrow that down to a subset of controllers by using attributes on the |
|
|
|
|
annotation, as the following example shows: |
|
|
|
|
`@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply only to the |
|
|
|
|
`@Controller` class, or class hierarchy, in which they are declared. If, instead, they |
|
|
|
|
are declared in an `@ControllerAdvice` or `@RestControllerAdvice` class, then they apply |
|
|
|
|
to any controller. Moreover, as of 5.3, `@ExceptionHandler` methods in `@ControllerAdvice` |
|
|
|
|
can be used to handle exceptions from any `@Controller` or any other handler. |
|
|
|
|
|
|
|
|
|
`@ControllerAdvice` is meta-annotated with `@Component` and therefore can be registered as |
|
|
|
|
a Spring bean through <<core.adoc#beans-java-instantiating-container-scan, |
|
|
|
|
component scanning>>. `@RestControllerAdvice` is meta-annotated with `@ControllerAdvice` |
|
|
|
|
and `@ResponseBody`, and that means `@ExceptionHandler` methods will have their return |
|
|
|
|
value rendered via response body message conversion, rather than via HTML views. |
|
|
|
|
|
|
|
|
|
On startup, `RequestMappingHandlerMapping` and `ExceptionHandlerExceptionResolver` detect |
|
|
|
|
controller advice beans and apply them at runtime. Global `@ExceptionHandler` methods, |
|
|
|
|
from an `@ControllerAdvice`, are applied _after_ local ones, from the `@Controller`. |
|
|
|
|
By contrast, global `@ModelAttribute` and `@InitBinder` methods are applied _before_ local ones. |
|
|
|
|
|
|
|
|
|
The `@ControllerAdvice` annotation has attributes that let you narrow the set of controllers |
|
|
|
|
and handlers that they apply to. For example: |
|
|
|
|
|
|
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
|
.Java |
|
|
|
|
|