diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index d2d9d8d95ad..7e5e2f64146 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -305,7 +305,8 @@ public class WebMvcAutoConfiguration { @Bean public WelcomePageHandlerMapping welcomePageHandlerMapping( ResourceProperties resourceProperties) { - return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage()); + return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), + this.mvcProperties.getStaticPathPattern()); } private void customizeResourceHandlerRegistration( @@ -524,8 +525,9 @@ public class WebMvcAutoConfiguration { private static final Log logger = LogFactory .getLog(WelcomePageHandlerMapping.class); - private WelcomePageHandlerMapping(Resource welcomePage) { - if (welcomePage != null) { + private WelcomePageHandlerMapping(Resource welcomePage, + String staticPathPattern) { + if (welcomePage != null && "/**".equals(staticPathPattern)) { logger.info("Adding welcome page: " + welcomePage); ParameterizableViewController controller = new ParameterizableViewController(); controller.setViewName("forward:index.html"); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index 37afea7dd25..2fdca55954a 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -572,6 +572,15 @@ public class WebMvcAutoConfigurationTests { .andExpect(status().isNotFound()); } + @Test + public void welcomePageRootHandlerIsNotRegisteredWhenStaticPathPatternIsNotSlashStarStar() { + load("spring.resources.static-locations:classpath:/welcome-page/", + "spring.mvc.static-path-pattern:/foo/**"); + WelcomePageHandlerMapping welcomePageHandlerMapping = this.context + .getBean(WelcomePageHandlerMapping.class); + assertThat(welcomePageHandlerMapping.getRootHandler()).isNull(); + } + @Test public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception { load("spring.resources.static-locations:classpath:/welcome-page/");