Merge branch '6.2.x'

This commit is contained in:
rstoyanchev 2025-06-06 15:28:43 +01:00
commit 6c27dbc095
9 changed files with 60 additions and 47 deletions

View File

@ -166,7 +166,7 @@ following sections to make this pattern much easier to implement.
== MockMvc and WebDriver Setup == MockMvc and WebDriver Setup
To use Selenium WebDriver with `MockMvc`, make sure that your project includes a test To use Selenium WebDriver with `MockMvc`, make sure that your project includes a test
dependency on `org.seleniumhq.selenium:selenium-htmlunit3-driver`. dependency on `org.seleniumhq.selenium:htmlunit3-driver`.
We can easily create a Selenium WebDriver that integrates with MockMvc by using the We can easily create a Selenium WebDriver that integrates with MockMvc by using the
`MockMvcHtmlUnitDriverBuilder` as the following example shows: `MockMvcHtmlUnitDriverBuilder` as the following example shows:

View File

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

View File

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

View File

@ -34,11 +34,7 @@ Controllers can then return a `Flux<List<B>>`; Reactor provides a dedicated oper
| `HttpHeaders` | `HttpHeaders`
| For returning a response with headers and no body. | For returning a response with headers and no body.
| `ErrorResponse` | `ErrorResponse`, `ProblemDetail`
| To render an RFC 9457 error response with details in the body,
see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses]
| `ProblemDetail`
| To render an RFC 9457 error response with details in the body, | To render an RFC 9457 error response with details in the body,
see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses] see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses]

View File

@ -10,18 +10,20 @@ to any controller. Moreover, as of 5.3, `@ExceptionHandler` methods in `@Control
can be used to handle exceptions from any `@Controller` or any other handler. 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 `@ControllerAdvice` is meta-annotated with `@Component` and therefore can be registered as
a Spring bean through xref:core/beans/java/instantiating-container.adoc#beans-java-instantiating-container-scan[component scanning] a Spring bean through xref:core/beans/java/instantiating-container.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 `@RestControllerAdvice` is a shortcut annotation that combines `@ControllerAdvice`
value rendered via response body message conversion, rather than via HTML views. with `@ResponseBody`, in effect simply an `@ControllerAdvice` whose exception handler
methods render to the response body.
On startup, `RequestMappingHandlerMapping` and `ExceptionHandlerExceptionResolver` detect On startup, `RequestMappingHandlerMapping` and `ExceptionHandlerExceptionResolver` detect
controller advice beans and apply them at runtime. Global `@ExceptionHandler` methods, controller advice beans and apply them at runtime. Global `@ExceptionHandler` methods,
from an `@ControllerAdvice`, are applied _after_ local ones, from the `@Controller`. from an `@ControllerAdvice`, are applied _after_ local ones, from the `@Controller`.
By contrast, global `@ModelAttribute` and `@InitBinder` methods are applied _before_ local ones. 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 By default, both `@ControllerAdvice` and `@RestControllerAdvice` apply to any controller,
and handlers that they apply to. For example: including `@Controller` and `@RestController`. Use attributes of the annotation to narrow
the set of controllers and handlers that they apply to. For example:
[tabs] [tabs]
====== ======

View File

@ -177,13 +177,9 @@ the content negotiation during the error handling phase will decide which conten
be converted through `HttpMessageConverter` instances and written to the response. be converted through `HttpMessageConverter` instances and written to the response.
See xref:web/webmvc/mvc-controller/ann-methods/responseentity.adoc[ResponseEntity]. See xref:web/webmvc/mvc-controller/ann-methods/responseentity.adoc[ResponseEntity].
| `ErrorResponse` | `ErrorResponse`, `ProblemDetail`
| To render an RFC 9457 error response with details in the body, | To render an RFC 9457 error response with details in the body,
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses] see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
| `ProblemDetail`
| To render an RFC 9457 error response with details in the body,
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
| `String` | `String`
| A view name to be resolved with `ViewResolver` implementations and used together with the | A view name to be resolved with `ViewResolver` implementations and used together with the

View File

@ -22,11 +22,7 @@ supported for all return values.
| `HttpHeaders` | `HttpHeaders`
| For returning a response with headers and no body. | For returning a response with headers and no body.
| `ErrorResponse` | `ErrorResponse`, `ProblemDetail`
| To render an RFC 9457 error response with details in the body,
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]
| `ProblemDetail`
| To render an RFC 9457 error response with details in the body, | To render an RFC 9457 error response with details in the body,
see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses] see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses]

View File

@ -71,23 +71,25 @@ import org.springframework.core.annotation.AliasFor;
* *
* <p>The following return types are supported for handler methods: * <p>The following return types are supported for handler methods:
* <ul> * <ul>
* <li>A {@code ModelAndView} object (from Servlet MVC). * <li>{@code ModelAndView} object (from Servlet MVC).
* <li>A {@link org.springframework.ui.Model} object, with the view name implicitly * <li>{@link org.springframework.ui.Model} object, with the view name implicitly
* determined through a {@link org.springframework.web.servlet.RequestToViewNameTranslator}. * determined through a {@link org.springframework.web.servlet.RequestToViewNameTranslator}.
* <li>A {@link java.util.Map} object for exposing a model, * <li>{@link java.util.Map} object for exposing a model,
* with the view name implicitly determined through a * with the view name implicitly determined through a
* {@link org.springframework.web.servlet.RequestToViewNameTranslator}. * {@link org.springframework.web.servlet.RequestToViewNameTranslator}.
* <li>A {@link org.springframework.web.servlet.View} object. * <li>{@link org.springframework.web.servlet.View} object.
* <li>A {@link String} value which is interpreted as view name. * <li>{@link String} value which is interpreted as view name.
* <li>{@link ResponseBody @ResponseBody} annotated methods (Servlet-only) * <li>{@link ResponseBody @ResponseBody} annotated methods (Servlet-only)
* to set the response content. The return value will be converted to the * to set the response content. The return value will be converted to the
* response stream using * response stream using
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
* <li>An {@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} or * <li>{@link org.springframework.http.HttpEntity HttpEntity&lt;?&gt;} or
* {@link org.springframework.http.ResponseEntity ResponseEntity&lt;?&gt;} object * {@link org.springframework.http.ResponseEntity ResponseEntity&lt;?&gt;} object
* (Servlet-only) to set response headers and content. The ResponseEntity body * (Servlet-only) to set response headers and content. The ResponseEntity body
* will be converted and written to the response stream using * will be converted and written to the response stream using
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
* <li>{@link org.springframework.http.ProblemDetail} or {@link org.springframework.web.ErrorResponse}
* object to render an RFC 9457 error response with details in the body.
* <li>{@code void} if the method handles the response itself (by * <li>{@code void} if the method handles the response itself (by
* writing the response content directly, declaring an argument of type * writing the response content directly, declaring an argument of type
* {@link jakarta.servlet.ServletResponse} / {@link jakarta.servlet.http.HttpServletResponse} * {@link jakarta.servlet.ServletResponse} / {@link jakarta.servlet.http.HttpServletResponse}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,18 +26,16 @@ import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.AliasFor;
/** /**
* A convenience annotation that is itself annotated with * A shortcut annotation that combines {@link ControllerAdvice @ControllerAdvice}
* {@link ControllerAdvice @ControllerAdvice} * with {@link ResponseBody @ResponseBody}, in effect simply an
* and {@link ResponseBody @ResponseBody}. * {@code @ControllerAdvice} whose exception handler methods render to the
* response body.
* *
* <p>Types that carry this annotation are treated as controller advice where * <p>By default, {@code @RestControllerAdvice} applies to any controller,
* {@link ExceptionHandler @ExceptionHandler} methods assume * including {@code @Controller} and {@code @RestController}. Use attributes of
* {@link ResponseBody @ResponseBody} semantics by default. * the annotation to apply more specific filtering criteria.
* *
* <p><b>NOTE:</b> {@code @RestControllerAdvice} is processed if an appropriate * <p>See {@link ControllerAdvice} for more details.
* {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
* {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter} pair
* which are the default in the MVC Java config and the MVC namespace.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen * @author Sam Brannen