Browse Source

Update docs for ControllerAdvice

In 5.3 it became possible to handle exceptions from any handler through
ExceptionHandler's in a ControllerAdvice class, but this is not
mentioned in the docs

See gh-22619, gh-27338
pull/27555/head
Rossen Stoyanchev 4 years ago
parent
commit
93f8706dd3
  1. 40
      src/docs/asciidoc/web/webmvc.adoc

40
src/docs/asciidoc/web/webmvc.adoc

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

Loading…
Cancel
Save