Optimize UrlPathHelper configuration for Spring MVC
Prior to this commit, Spring Boot would auto-configure Spring MVC and would keep the default `UrlPathHelper` configuration. Since Spring Boot is in charge of configuring the `DispatcherServlet` and its mapping, it is in a position to optimally configure the `UrlPathHelper` depending on the chosen mapping. This commit sets the `alwaysUseFullPath` property of `UrlPathHelper` if the Servlet mapping is `"/"`. This is more efficient since this configuration requires less processing of the request path. Closes gh-21499
This commit is contained in:
@@ -119,6 +119,7 @@ import org.springframework.web.servlet.resource.VersionResourceResolver;
|
||||
import org.springframework.web.servlet.view.BeanNameViewResolver;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link EnableWebMvc Web MVC}.
|
||||
@@ -188,16 +189,20 @@ public class WebMvcAutoConfiguration {
|
||||
|
||||
private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
|
||||
|
||||
private final ObjectProvider<DispatcherServletPath> dispatcherServletPath;
|
||||
|
||||
final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
|
||||
|
||||
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,
|
||||
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
|
||||
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider) {
|
||||
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
|
||||
ObjectProvider<DispatcherServletPath> dispatcherServletPath) {
|
||||
this.resourceProperties = resourceProperties;
|
||||
this.mvcProperties = mvcProperties;
|
||||
this.beanFactory = beanFactory;
|
||||
this.messageConvertersProvider = messageConvertersProvider;
|
||||
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
|
||||
this.dispatcherServletPath = dispatcherServletPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -227,6 +232,14 @@ public class WebMvcAutoConfiguration {
|
||||
configurer.setUseSuffixPatternMatch(this.mvcProperties.getPathmatch().isUseSuffixPattern());
|
||||
configurer.setUseRegisteredSuffixPatternMatch(
|
||||
this.mvcProperties.getPathmatch().isUseRegisteredSuffixPattern());
|
||||
this.dispatcherServletPath.ifAvailable((dispatcherPath) -> {
|
||||
String servletUrlMapping = dispatcherPath.getServletUrlMapping();
|
||||
if (servletUrlMapping.equals("/")) {
|
||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||
urlPathHelper.setAlwaysUseFullPath(true);
|
||||
configurer.setUrlPathHelper(urlPathHelper);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user