Polish "Add support for MVC router functions to mappings endpoint"

See gh-44163
This commit is contained in:
Andy Wilkinson 2025-02-13 11:15:41 +00:00
parent a747bcf6d4
commit b2ba2a53fe
5 changed files with 28 additions and 15 deletions

View File

@ -63,6 +63,7 @@ import static org.springframework.web.servlet.function.RequestPredicates.GET;
* Tests for generating documentation describing {@link MappingsEndpoint}.
*
* @author Andy Wilkinson
* @author Xiong Tang
*/
@ExtendWith(RestDocumentationExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ -136,10 +137,10 @@ class MappingsEndpointServletDocumentationTests extends AbstractEndpointDocument
.description("Descriptor of the method as specified in the Java Language Specification."));
List<FieldDescriptor> handlerFunction = List.of(
fieldWithPath("*.[].details.handlerFunction").optional()
.type(JsonFieldType.OBJECT)
.description("Details of the function, if any, that will handle requests to this mapping."),
.type(JsonFieldType.OBJECT)
.description("Details of the function, if any, that will handle requests to this mapping."),
fieldWithPath("*.[].details.handlerFunction.className").type(JsonFieldType.STRING)
.description("Fully qualified name of the class of the function."));
.description("Fully qualified name of the class of the function."));
dispatcherServletFields.addAll(handlerFunction);
dispatcherServletFields.addAll(handlerMethod);
dispatcherServletFields.addAll(requestMappingConditions);

View File

@ -23,6 +23,7 @@ import org.springframework.web.servlet.DispatcherServlet;
* Details of a {@link DispatcherServlet} mapping.
*
* @author Andy Wilkinson
* @author Xiong Tang
* @since 2.0.0
*/
public class DispatcherServletMappingDetails {

View File

@ -60,6 +60,7 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMappi
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Xiong Tang
* @since 2.0.0
*/
@ImportRuntimeHints(DispatcherServletsMappingDescriptionProviderRuntimeHints.class)

View File

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.web.mappings.servlet;
import org.springframework.web.servlet.function.HandlerFunction;
/**

View File

@ -57,6 +57,8 @@ import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
@ -71,6 +73,7 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Xiong Tang
*/
class MappingsEndpointTests {
@ -88,7 +91,7 @@ class MappingsEndpointTests {
"dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets.get("dispatcherServlet");
assertThat(handlerMappings).hasSize(3);
assertThat(handlerMappings).hasSize(4);
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
assertThat(servlets).hasSize(1);
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
@ -111,7 +114,7 @@ class MappingsEndpointTests {
"dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets.get("dispatcherServlet");
assertThat(handlerMappings).hasSize(3);
assertThat(handlerMappings).hasSize(4);
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
assertThat(servlets).hasSize(1);
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
@ -131,9 +134,9 @@ class MappingsEndpointTests {
"dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet",
"customDispatcherServletRegistration", "anotherDispatcherServletRegistration");
assertThat(dispatcherServlets.get("dispatcherServlet")).hasSize(3);
assertThat(dispatcherServlets.get("customDispatcherServletRegistration")).hasSize(3);
assertThat(dispatcherServlets.get("anotherDispatcherServletRegistration")).hasSize(3);
assertThat(dispatcherServlets.get("dispatcherServlet")).hasSize(4);
assertThat(dispatcherServlets.get("customDispatcherServletRegistration")).hasSize(4);
assertThat(dispatcherServlets.get("anotherDispatcherServletRegistration")).hasSize(4);
});
}
@ -248,18 +251,26 @@ class MappingsEndpointTests {
return dispatcherServlet;
}
@Bean
org.springframework.web.servlet.function.RouterFunction<org.springframework.web.servlet.function.ServerResponse> routerFunction() {
return RouterFunctions
.route(RequestPredicates.GET("/one"),
(request) -> org.springframework.web.servlet.function.ServerResponse.ok().build())
.andRoute(RequestPredicates.POST("/two"),
(request) -> org.springframework.web.servlet.function.ServerResponse.ok().build());
}
@RequestMapping("/three")
void three() {
}
@Bean
org.springframework.web.servlet.function.RouterFunction<org.springframework.web.servlet.function.ServerResponse> routerFunction() {
return org.springframework.web.servlet.function.RouterFunctions
.route(org.springframework.web.servlet.function.RequestPredicates.GET("/one"),
(request) -> org.springframework.web.servlet.function.ServerResponse.ok().build())
.andRoute(org.springframework.web.servlet.function.RequestPredicates.POST("/two"),
(request) -> org.springframework.web.servlet.function.ServerResponse.ok().build());
org.springframework.web.servlet.function.RouterFunction<org.springframework.web.servlet.function.ServerResponse> routerFunctionWithAttributes() {
return RouterFunctions
.route(RequestPredicates.GET("/four"),
(request) -> org.springframework.web.servlet.function.ServerResponse.ok().build())
.withAttribute("test", "test");
}
}