diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java index 6bebaa18df0..c3b26415889 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java @@ -187,7 +187,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi @Override @SuppressWarnings("deprecation") public void afterPropertiesSet() { - this.config = new RequestMappingInfo.BuilderConfiguration(); this.config.setTrailingSlashMatch(useTrailingSlashMatch()); this.config.setContentNegotiationManager(getContentNegotiationManager()); @@ -247,17 +246,19 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi } /** - * Get the configuration to build {@link RequestMappingInfo} - * instances. This is useful for programmatic registration of - * additional mappings following the same configuration as {@link - * #createRequestMappingInfo(RequestMapping, RequestCondition)}. - * - * @return builder configuration to be supplied into {@link RequestMappingInfo.Builder#options}. + * Obtain a {@link RequestMappingInfo.BuilderConfiguration} that can reflects + * the internal configuration of this {@code HandlerMapping} and can be used + * to set {@link RequestMappingInfo.Builder#options(RequestMappingInfo.BuilderConfiguration)}. + *

This is useful for programmatic registration of request mappings via + * {@link #registerHandlerMethod(Object, Method, RequestMappingInfo)}. + * @return the builder configuration that reflects the internal state + * @since 5.3.14 */ - public RequestMappingInfo.BuilderConfiguration getRequestMappingInfoBuilderConfiguration() { + public RequestMappingInfo.BuilderConfiguration getBuilderConfiguration() { return this.config; } + /** * {@inheritDoc} *

Expects a handler to have either a type-level @{@link Controller} @@ -401,6 +402,19 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi updateConsumesCondition(mapping, method); } + /** + * {@inheritDoc} + *

Note: To create the {@link RequestMappingInfo}, + * please use {@link #getBuilderConfiguration()} and set the options on + * {@link RequestMappingInfo.Builder#options(RequestMappingInfo.BuilderConfiguration)} + * to match how this {@code HandlerMapping} is configured. This + * is important for example to ensure use of + * {@link org.springframework.web.util.pattern.PathPattern} or + * {@link org.springframework.util.PathMatcher} based matching. + * @param handler the bean name of the handler or the handler instance + * @param method the method to register + * @param mapping the mapping conditions associated with the handler method + */ @Override protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) { super.registerHandlerMethod(handler, method, mapping); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java index 2e6149bc969..9ed74e07817 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -80,15 +80,15 @@ public class RequestMappingHandlerMappingTests { } @Test - void getRequestMappingInfoBuilderConfiguration() { - RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping(); - handlerMapping.setApplicationContext(new StaticWebApplicationContext()); + void builderConfiguration() { + RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping(); + mapping.setApplicationContext(new StaticWebApplicationContext()); - RequestMappingInfo.BuilderConfiguration beforeAfterPropertiesSet = handlerMapping.getRequestMappingInfoBuilderConfiguration(); - assertThat(beforeAfterPropertiesSet).isNotNull(); - handlerMapping.afterPropertiesSet(); - RequestMappingInfo.BuilderConfiguration afterPropertiesSet = handlerMapping.getRequestMappingInfoBuilderConfiguration(); - assertThat(afterPropertiesSet).isNotNull().isNotSameAs(beforeAfterPropertiesSet); + RequestMappingInfo.BuilderConfiguration config = mapping.getBuilderConfiguration(); + assertThat(config).isNotNull(); + + mapping.afterPropertiesSet(); + assertThat(mapping.getBuilderConfiguration()).isNotNull().isNotSameAs(config); } @Test @@ -99,7 +99,8 @@ public class RequestMappingHandlerMappingTests { handlerMapping.setApplicationContext(new StaticWebApplicationContext()); Map fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON); - org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy = new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions); + org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy = + new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions); ContentNegotiationManager manager = new ContentNegotiationManager(strategy); handlerMapping.setContentNegotiationManager(manager); @@ -115,7 +116,8 @@ public class RequestMappingHandlerMappingTests { @SuppressWarnings("deprecation") void useRegisteredSuffixPatternMatchInitialization() { Map fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON); - org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy = new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions); + org.springframework.web.accept.PathExtensionContentNegotiationStrategy strategy = + new org.springframework.web.accept.PathExtensionContentNegotiationStrategy(fileExtensions); ContentNegotiationManager manager = new ContentNegotiationManager(strategy); final Set extensions = new HashSet<>();