Skip to content

Commit ca93c8d

Browse files
committed
Merge branch '2.3.x'
Closes gh-22856
2 parents 91934b5 + 1270af9 commit ca93c8d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.Format;
5757
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5858
import org.springframework.boot.convert.ApplicationConversionService;
59+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5960
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
6061
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
6162
import org.springframework.boot.web.servlet.filter.OrderedRequestContextFilter;
@@ -191,18 +192,22 @@ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
191192

192193
private final ObjectProvider<DispatcherServletPath> dispatcherServletPath;
193194

195+
private final ObjectProvider<ServletRegistrationBean<?>> servletRegistrations;
196+
194197
final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
195198

196199
public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties,
197200
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
198201
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
199-
ObjectProvider<DispatcherServletPath> dispatcherServletPath) {
202+
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
203+
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
200204
this.resourceProperties = resourceProperties;
201205
this.mvcProperties = mvcProperties;
202206
this.beanFactory = beanFactory;
203207
this.messageConvertersProvider = messageConvertersProvider;
204208
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
205209
this.dispatcherServletPath = dispatcherServletPath;
210+
this.servletRegistrations = servletRegistrations;
206211
this.mvcProperties.checkConfiguration();
207212
}
208213

@@ -239,14 +244,19 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
239244
this.mvcProperties.getPathmatch().isUseRegisteredSuffixPattern());
240245
this.dispatcherServletPath.ifAvailable((dispatcherPath) -> {
241246
String servletUrlMapping = dispatcherPath.getServletUrlMapping();
242-
if (servletUrlMapping.equals("/")) {
247+
if (servletUrlMapping.equals("/") && singleDispatcherServlet()) {
243248
UrlPathHelper urlPathHelper = new UrlPathHelper();
244249
urlPathHelper.setAlwaysUseFullPath(true);
245250
configurer.setUrlPathHelper(urlPathHelper);
246251
}
247252
});
248253
}
249254

255+
private boolean singleDispatcherServlet() {
256+
return this.servletRegistrations.stream().map(ServletRegistrationBean::getServlet)
257+
.filter(DispatcherServlet.class::isInstance).count() == 1;
258+
}
259+
250260
@Override
251261
@SuppressWarnings("deprecation")
252262
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
5454
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
5555
import org.springframework.boot.web.servlet.FilterRegistrationBean;
56+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5657
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
5758
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
5859
import org.springframework.context.ApplicationContext;
@@ -84,6 +85,7 @@
8485
import org.springframework.web.filter.FormContentFilter;
8586
import org.springframework.web.filter.HiddenHttpMethodFilter;
8687
import org.springframework.web.filter.RequestContextFilter;
88+
import org.springframework.web.servlet.DispatcherServlet;
8789
import org.springframework.web.servlet.HandlerAdapter;
8890
import org.springframework.web.servlet.HandlerExceptionResolver;
8991
import org.springframework.web.servlet.HandlerMapping;
@@ -893,6 +895,23 @@ void urlPathHelperDoesNotUseFullPathWithServletMapping() {
893895
});
894896
}
895897

898+
@Test
899+
void urlPathHelperDoesNotUseFullPathWithAdditionalDispatcherServlet() {
900+
this.contextRunner.withUserConfiguration(AdditionalDispatcherServletConfiguration.class).run((context) -> {
901+
UrlPathHelper urlPathHelper = context.getBean(UrlPathHelper.class);
902+
assertThat(urlPathHelper).extracting("alwaysUseFullPath").isEqualTo(false);
903+
});
904+
}
905+
906+
@Test
907+
void urlPathHelperDoesNotUseFullPathWithAdditionalUntypedDispatcherServlet() {
908+
this.contextRunner.withUserConfiguration(AdditionalUntypedDispatcherServletConfiguration.class)
909+
.run((context) -> {
910+
UrlPathHelper urlPathHelper = context.getBean(UrlPathHelper.class);
911+
assertThat(urlPathHelper).extracting("alwaysUseFullPath").isEqualTo(false);
912+
});
913+
}
914+
896915
private void assertCacheControl(AssertableWebApplicationContext context) {
897916
Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
898917
assertThat(handlerMap).hasSize(2);
@@ -1276,4 +1295,24 @@ public void addCorsMappings(CorsRegistry registry) {
12761295

12771296
}
12781297

1298+
@Configuration(proxyBeanMethods = false)
1299+
static class AdditionalDispatcherServletConfiguration {
1300+
1301+
@Bean
1302+
ServletRegistrationBean<DispatcherServlet> additionalDispatcherServlet() {
1303+
return new ServletRegistrationBean<>(new DispatcherServlet());
1304+
}
1305+
1306+
}
1307+
1308+
@Configuration(proxyBeanMethods = false)
1309+
static class AdditionalUntypedDispatcherServletConfiguration {
1310+
1311+
@Bean
1312+
ServletRegistrationBean<?> additionalDispatcherServlet() {
1313+
return new ServletRegistrationBean<>(new DispatcherServlet());
1314+
}
1315+
1316+
}
1317+
12791318
}

0 commit comments

Comments
 (0)