Skip to content

Commit

Permalink
Polish "Add support for MVC router functions to mappings endpoint"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Feb 13, 2025
1 parent a747bcf commit b2ba2a5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* Tests for generating documentation describing {@link MappingsEndpoint}.
*
* @author Andy Wilkinson
* @author Xiong Tang
*/
@ExtendWith(RestDocumentationExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
Expand Down Expand Up @@ -136,10 +137,10 @@ void mappings() {
.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Details of a {@link DispatcherServlet} mapping.
*
* @author Andy Wilkinson
* @author Xiong Tang
* @since 2.0.0
*/
public class DispatcherServletMappingDetails {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Xiong Tang
* @since 2.0.0
*/
@ImportRuntimeHints(DispatcherServletsMappingDescriptionProviderRuntimeHints.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.actuate.web.mappings.servlet;


import org.springframework.web.servlet.function.HandlerFunction;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
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;
Expand All @@ -71,6 +73,7 @@
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Xiong Tang
*/
class MappingsEndpointTests {

Expand All @@ -88,7 +91,7 @@ void servletWebMappings() {
"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");
Expand All @@ -111,7 +114,7 @@ void servletWebMappingsWithPathPatternParser() {
"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");
Expand All @@ -131,9 +134,9 @@ void servletWebMappingsWithAdditionalDispatcherServlets() {
"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);
});
}

Expand Down Expand Up @@ -248,18 +251,26 @@ DispatcherServlet dispatcherServlet(WebApplicationContext context) throws Servle
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");
}

}
Expand Down

0 comments on commit b2ba2a5

Please sign in to comment.