From fa781c8390e03ddc3fff56fc964335b651a162f2 Mon Sep 17 00:00:00 2001 From: Jimmy Axenhus Date: Mon, 28 Apr 2025 16:28:27 +0200 Subject: [PATCH 1/5] Correct name of Selenium WebDriver artifact The selenium-htmlunit3-driver artifact does not exist. Closes gh-34840 Signed-off-by: Jimmy Axenhus --- .../modules/ROOT/pages/testing/mockmvc/htmlunit/webdriver.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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: From 4d862b871d0502a7944953ef8a70f382a59d618a Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Fri, 7 Mar 2025 18:20:47 +0100 Subject: [PATCH 2/5] Improve Javadoc for @ExceptionHandler This commit adds `ProblemDetail` and `ErrorResponse` to the list of supported return types for `@ExceptionHandler` methods. Closes gh-34554 Signed-off-by: Vedran Pavic --- .../springframework/web/bind/annotation/ExceptionHandler.java | 2 ++ 1 file changed, 2 insertions(+) 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..ece0b98f609 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 @@ -88,6 +88,8 @@ import org.springframework.core.annotation.AliasFor; * (Servlet-only) to set response headers and content. The ResponseEntity body * will be converted and written to the response stream using * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. + *
  • A {@link org.springframework.http.ProblemDetail} or {@link org.springframework.web.ErrorResponse} + * object to render an RFC 9457 error response with details in the body. *
  • {@code void} if the method handles the response itself (by * writing the response content directly, declaring an argument of type * {@link jakarta.servlet.ServletResponse} / {@link jakarta.servlet.http.HttpServletResponse} From de520909591c99107aa33335d33359f207624359 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 6 Jun 2025 14:16:26 +0100 Subject: [PATCH 3/5] Polishing contribution Closes gh-34554 --- .../controller/ann-methods/return-types.adoc | 6 +----- .../mvc-controller/ann-exceptionhandler.adoc | 8 ++------ .../mvc-controller/ann-methods/return-types.adoc | 6 +----- .../web/bind/annotation/ExceptionHandler.java | 14 +++++++------- 4 files changed, 11 insertions(+), 23 deletions(-) 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-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 ece0b98f609..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,24 +71,24 @@ import org.springframework.core.annotation.AliasFor; * *

    The following return types are supported for handler methods: *

      - *
    • A {@code ModelAndView} object (from Servlet MVC). - *
    • A {@link org.springframework.ui.Model} object, with the view name implicitly + *
    • {@code ModelAndView} object (from Servlet MVC). + *
    • {@link org.springframework.ui.Model} object, with the view name implicitly * determined through a {@link org.springframework.web.servlet.RequestToViewNameTranslator}. - *
    • A {@link java.util.Map} object for exposing a model, + *
    • {@link java.util.Map} object for exposing a model, * with the view name implicitly determined through a * {@link org.springframework.web.servlet.RequestToViewNameTranslator}. - *
    • A {@link org.springframework.web.servlet.View} object. - *
    • A {@link String} value which is interpreted as view name. + *
    • {@link org.springframework.web.servlet.View} object. + *
    • {@link String} value which is interpreted as view name. *
    • {@link ResponseBody @ResponseBody} annotated methods (Servlet-only) * to set the response content. The return value will be converted to the * response stream using * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. - *
    • An {@link org.springframework.http.HttpEntity HttpEntity<?>} or + *
    • {@link org.springframework.http.HttpEntity HttpEntity<?>} or * {@link org.springframework.http.ResponseEntity ResponseEntity<?>} object * (Servlet-only) to set response headers and content. The ResponseEntity body * will be converted and written to the response stream using * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. - *
    • A {@link org.springframework.http.ProblemDetail} or {@link org.springframework.web.ErrorResponse} + *
    • {@link org.springframework.http.ProblemDetail} or {@link org.springframework.web.ErrorResponse} * object to render an RFC 9457 error response with details in the body. *
    • {@code void} if the method handles the response itself (by * writing the response content directly, declaring an argument of type From 4782c697b85a8d3368d6d3c58ee4c5b2652ebb29 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 6 Jun 2025 14:40:54 +0100 Subject: [PATCH 4/5] Improve RestControllerAdvice documentation Closes gh-34866 --- .../web/webmvc/mvc-controller/ann-advice.adoc | 14 +++++++------ .../bind/annotation/RestControllerAdvice.java | 20 +++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) 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/spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java index c819ee0a4da..ea0c71cecc7 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RestControllerAdvice.java @@ -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"); * 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; /** - * A convenience annotation that is itself annotated with - * {@link ControllerAdvice @ControllerAdvice} - * and {@link ResponseBody @ResponseBody}. + * A shortcut annotation that combines {@link ControllerAdvice @ControllerAdvice} + * with {@link ResponseBody @ResponseBody}, in effect simply an + * {@code @ControllerAdvice} whose exception handler methods render to the + * response body. * - *

      Types that carry this annotation are treated as controller advice where - * {@link ExceptionHandler @ExceptionHandler} methods assume - * {@link ResponseBody @ResponseBody} semantics by default. + *

      By default, {@code @RestControllerAdvice} applies to any controller, + * including {@code @Controller} and {@code @RestController}. Use attributes of + * the annotation to apply more specific filtering criteria. * - *

      NOTE: {@code @RestControllerAdvice} is processed if an appropriate - * {@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. + *

      See {@link ControllerAdvice} for more details. * * @author Rossen Stoyanchev * @author Sam Brannen From c769f43b3e8a2bebdc17d2a8f9d83f93ac60e20c Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 6 Jun 2025 15:28:18 +0100 Subject: [PATCH 5/5] Update docs on testing client code Closes gh-34892 --- .../pages/testing/spring-mvc-test-client.adoc | 24 +++++++++++++++---- .../web/webflux-webclient/client-testing.adoc | 13 +++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) 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`]