Skip to content

Commit 6c27dbc

Browse files
committed
Merge branch '6.2.x'
2 parents 583612f + c769f43 commit 6c27dbc

File tree

9 files changed

+60
-47
lines changed

9 files changed

+60
-47
lines changed

framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ following sections to make this pattern much easier to implement.
166166
== MockMvc and WebDriver Setup
167167

168168
To use Selenium WebDriver with `MockMvc`, make sure that your project includes a test
169-
dependency on `org.seleniumhq.selenium:selenium-htmlunit3-driver`.
169+
dependency on `org.seleniumhq.selenium:htmlunit3-driver`.
170170

171171
We can easily create a Selenium WebDriver that integrates with MockMvc by using the
172172
`MockMvcHtmlUnitDriverBuilder` as the following example shows:

framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
[[spring-mvc-test-client]]
22
= Testing Client Applications
33

4-
You can use client-side tests to test code that internally uses the `RestTemplate`. The
5-
idea is to declare expected requests and to provide "`stub`" responses so that you can
6-
focus on testing the code in isolation (that is, without running a server). The following
7-
example shows how to do so:
4+
To test code that uses the `RestClient` or `RestTemplate`, you can use a mock web server, such as
5+
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or
6+
https://wiremock.org/[WireMock]. Mock web servers accept requests over HTTP like a regular
7+
server, and that means you can test with the same HTTP client that is also configured in
8+
the same way as in production, which is important because there are often subtle
9+
differences in the way different clients handle network I/O. Another advantage of mock
10+
web servers is the ability to simulate specific network issues and conditions at the
11+
transport level, in combination with the client used in production.
12+
13+
In addition to dedicated mock web servers, historically the Spring Framework has provided
14+
a built-in option to test `RestClient` or `RestTemplate` through `MockRestServiceServer`.
15+
This relies on configuring the client under test with a custom `ClientHttpRequestFactory`
16+
backed by the mock server that is in turn set up to expect requests and send "`stub`"
17+
responses so that you can focus on testing the code in isolation, without running a server.
18+
19+
TIP: `MockRestServiceServer` predates the existence of mock web servers. At present, we
20+
recommend using mock web servers for more complete testing of the transport layer and
21+
network conditions.
22+
23+
The following example shows an example of using `MockRestServiceServer`:
824

925
[tabs]
1026
======

framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
= Testing
33
:page-section-summary-toc: 1
44

5-
To test code that uses the `WebClient`, you can use a mock web server, such as the
6-
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see an example
7-
of its use, check out
5+
To test code that uses the `WebClient`, you can use a mock web server, such as
6+
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or
7+
https://wiremock.org/[WireMock]. Mock web servers accept requests over HTTP like a regular
8+
server, and that means you can test with the same HTTP client that is also configured in
9+
the same way as in production, which is important because there are often subtle
10+
differences in the way different clients handle network I/O. Another advantage of mock
11+
web servers is the ability to simulate specific network issues and conditions at the
12+
transport level, in combination with the client used in production.
13+
14+
For example use of MockWebServer, see
815
{spring-framework-code}/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java[`WebClientIntegrationTests`]
916
in the Spring Framework test suite or the
1017
https://github.com/square/okhttp/tree/master/samples/static-server[`static-server`]

framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ Controllers can then return a `Flux<List<B>>`; Reactor provides a dedicated oper
3434
| `HttpHeaders`
3535
| For returning a response with headers and no body.
3636

37-
| `ErrorResponse`
38-
| To render an RFC 9457 error response with details in the body,
39-
see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses]
40-
41-
| `ProblemDetail`
37+
| `ErrorResponse`, `ProblemDetail`
4238
| To render an RFC 9457 error response with details in the body,
4339
see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses]
4440

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
======

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,9 @@ the content negotiation during the error handling phase will decide which conten
177177
be converted through `HttpMessageConverter` instances and written to the response.
178178
See xref:web/webmvc/mvc-controller/ann-methods/responseentity.adoc[ResponseEntity].
179179

180-
| `ErrorResponse`
180+
| `ErrorResponse`, `ProblemDetail`
181181
| To render an RFC 9457 error response with details in the body,
182-
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
183-
184-
| `ProblemDetail`
185-
| To render an RFC 9457 error response with details in the body,
186-
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
182+
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
187183

188184
| `String`
189185
| A view name to be resolved with `ViewResolver` implementations and used together with the

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ supported for all return values.
2222
| `HttpHeaders`
2323
| For returning a response with headers and no body.
2424

25-
| `ErrorResponse`
26-
| To render an RFC 9457 error response with details in the body,
27-
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
28-
29-
| `ProblemDetail`
25+
| `ErrorResponse`, `ProblemDetail`
3026
| To render an RFC 9457 error response with details in the body,
3127
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
3228

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,25 @@
7171
*
7272
* <p>The following return types are supported for handler methods:
7373
* <ul>
74-
* <li>A {@code ModelAndView} object (from Servlet MVC).
75-
* <li>A {@link org.springframework.ui.Model} object, with the view name implicitly
74+
* <li>{@code ModelAndView} object (from Servlet MVC).
75+
* <li>{@link org.springframework.ui.Model} object, with the view name implicitly
7676
* determined through a {@link org.springframework.web.servlet.RequestToViewNameTranslator}.
77-
* <li>A {@link java.util.Map} object for exposing a model,
77+
* <li>{@link java.util.Map} object for exposing a model,
7878
* with the view name implicitly determined through a
7979
* {@link org.springframework.web.servlet.RequestToViewNameTranslator}.
80-
* <li>A {@link org.springframework.web.servlet.View} object.
81-
* <li>A {@link String} value which is interpreted as view name.
80+
* <li>{@link org.springframework.web.servlet.View} object.
81+
* <li>{@link String} value which is interpreted as view name.
8282
* <li>{@link ResponseBody @ResponseBody} annotated methods (Servlet-only)
8383
* to set the response content. The return value will be converted to the
8484
* response stream using
8585
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
86-
* <li>An {@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} or
86+
* <li>{@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} or
8787
* {@link org.springframework.http.ResponseEntity ResponseEntity&lt;?&gt;} object
8888
* (Servlet-only) to set response headers and content. The ResponseEntity body
8989
* will be converted and written to the response stream using
9090
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
91+
* <li>{@link org.springframework.http.ProblemDetail} or {@link org.springframework.web.ErrorResponse}
92+
* object to render an RFC 9457 error response with details in the body.
9193
* <li>{@code void} if the method handles the response itself (by
9294
* writing the response content directly, declaring an argument of type
9395
* {@link jakarta.servlet.ServletResponse} / {@link jakarta.servlet.http.HttpServletResponse}

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)