Skip to content

Commit 4782c69

Browse files
committed
Improve RestControllerAdvice documentation
Closes gh-34866
1 parent de52090 commit 4782c69

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-advice.adoc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ to any controller. Moreover, as of 5.3, `@ExceptionHandler` methods in `@Control
1010
can be used to handle exceptions from any `@Controller` or any other handler.
1111

1212
`@ControllerAdvice` is meta-annotated with `@Component` and therefore can be registered as
13-
a Spring bean through xref:core/beans/java/instantiating-container.adoc#beans-java-instantiating-container-scan[component scanning]
14-
. `@RestControllerAdvice` is meta-annotated with `@ControllerAdvice`
15-
and `@ResponseBody`, and that means `@ExceptionHandler` methods will have their return
16-
value rendered via response body message conversion, rather than via HTML views.
13+
a Spring bean through xref:core/beans/java/instantiating-container.adoc#beans-java-instantiating-container-scan[component scanning].
14+
15+
`@RestControllerAdvice` is a shortcut annotation that combines `@ControllerAdvice`
16+
with `@ResponseBody`, in effect simply an `@ControllerAdvice` whose exception handler
17+
methods render to the response body.
1718

1819
On startup, `RequestMappingHandlerMapping` and `ExceptionHandlerExceptionResolver` detect
1920
controller advice beans and apply them at runtime. Global `@ExceptionHandler` methods,
2021
from an `@ControllerAdvice`, are applied _after_ local ones, from the `@Controller`.
2122
By contrast, global `@ModelAttribute` and `@InitBinder` methods are applied _before_ local ones.
2223

23-
The `@ControllerAdvice` annotation has attributes that let you narrow the set of controllers
24-
and handlers that they apply to. For example:
24+
By default, both `@ControllerAdvice` and `@RestControllerAdvice` apply to any controller,
25+
including `@Controller` and `@RestController`. Use attributes of the annotation to narrow
26+
the set of controllers and handlers that they apply to. For example:
2527

2628
[tabs]
2729
======

spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,18 +26,16 @@
2626
import org.springframework.core.annotation.AliasFor;
2727

2828
/**
29-
* A convenience annotation that is itself annotated with
30-
* {@link ControllerAdvice @ControllerAdvice}
31-
* and {@link ResponseBody @ResponseBody}.
29+
* A shortcut annotation that combines {@link ControllerAdvice @ControllerAdvice}
30+
* with {@link ResponseBody @ResponseBody}, in effect simply an
31+
* {@code @ControllerAdvice} whose exception handler methods render to the
32+
* response body.
3233
*
33-
* <p>Types that carry this annotation are treated as controller advice where
34-
* {@link ExceptionHandler @ExceptionHandler} methods assume
35-
* {@link ResponseBody @ResponseBody} semantics by default.
34+
* <p>By default, {@code @RestControllerAdvice} applies to any controller,
35+
* including {@code @Controller} and {@code @RestController}. Use attributes of
36+
* the annotation to apply more specific filtering criteria.
3637
*
37-
* <p><b>NOTE:</b> {@code @RestControllerAdvice} is processed if an appropriate
38-
* {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
39-
* {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter} pair
40-
* which are the default in the MVC Java config and the MVC namespace.
38+
* <p>See {@link ControllerAdvice} for more details.
4139
*
4240
* @author Rossen Stoyanchev
4341
* @author Sam Brannen

0 commit comments

Comments
 (0)