diff --git a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc index 175af0f55ee..d3a8df109d3 100644 --- a/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc +++ b/framework-docs/modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc @@ -166,7 +166,7 @@ following sections to make this pattern much easier to implement. == MockMvc and WebDriver Setup 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 `MockMvcHtmlUnitDriverBuilder` as the following example shows: diff --git a/framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc b/framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc index 55738c214f7..b145277f21c 100644 --- a/framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc +++ b/framework-docs/modules/ROOT/pages/testing/spring-mvc-test-client.adoc @@ -1,10 +1,26 @@ [[spring-mvc-test-client]] = Testing Client Applications -You can use client-side tests to test code that internally uses the `RestTemplate`. The -idea is to declare expected requests and to provide "`stub`" responses so that you can -focus on testing the code in isolation (that is, without running a server). The following -example shows how to do so: +To test code that uses the `RestClient` or `RestTemplate`, you can use a mock web server, such as +https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or +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. + +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] ====== diff --git a/framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc b/framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc index febbb549827..f7a69c7dbb3 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux-webclient/client-testing.adoc @@ -2,9 +2,16 @@ = Testing :page-section-summary-toc: 1 -To test code that uses the `WebClient`, you can use a mock web server, such as the -https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see an example -of its use, check out +To test code that uses the `WebClient`, you can use a mock web server, such as +https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer] or +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`] in the Spring Framework test suite or the https://github.com/square/okhttp/tree/master/samples/static-server[`static-server`] diff --git a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc index 22fe3570b6c..450b9e99258 100644 --- a/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc +++ b/framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/return-types.adoc @@ -34,11 +34,7 @@ Controllers can then return a `Flux>`; Reactor provides a dedicated oper | `HttpHeaders` | For returning a response with headers and no body. -| `ErrorResponse` -| To render an RFC 9457 error response with details in the body, - see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses] - -| `ProblemDetail` +| `ErrorResponse`, `ProblemDetail` | To render an RFC 9457 error response with details in the body, see xref:web/webflux/ann-rest-exceptions.adoc[Error Responses] diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-advice.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-advice.adoc index 403db0bbf2a..f4af7ac1b4e 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-advice.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-advice.adoc @@ -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. `@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] -. `@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. +a Spring bean through xref:core/beans/java/instantiating-container.adoc#beans-java-instantiating-container-scan[component scanning]. + +`@RestControllerAdvice` is a shortcut annotation that combines `@ControllerAdvice` +with `@ResponseBody`, in effect simply an `@ControllerAdvice` whose exception handler +methods render to the response body. 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: +By default, both `@ControllerAdvice` and `@RestControllerAdvice` apply to any controller, +including `@Controller` and `@RestController`. Use attributes of the annotation to narrow +the set of controllers and handlers that they apply to. For example: [tabs] ====== diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-exceptionhandler.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-exceptionhandler.adoc index e13037ded80..3a0f94e5720 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-exceptionhandler.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-exceptionhandler.adoc @@ -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. 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, -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] + see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses] | `String` | A view name to be resolved with `ViewResolver` implementations and used together with the diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc index 557de2db3ca..bd6aa862ec1 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/return-types.adoc @@ -22,11 +22,7 @@ supported for all return values. | `HttpHeaders` | For returning a response with headers and no body. -| `ErrorResponse` -| To render an RFC 9457 error response with details in the body, - see xref:web/webmvc/mvc-ann-rest-exceptions.adoc[Error Responses] - -| `ProblemDetail` +| `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] diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandler.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandler.java index ed19bdf6041..64859e16c2d 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandler.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/ExceptionHandler.java @@ -71,23 +71,25 @@ import org.springframework.core.annotation.AliasFor; * *

The following return types are supported for handler methods: *