From 220f8cdca29810dd5c2d6561f66c6d89ab13b8b1 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 14 Mar 2018 17:16:39 +0100 Subject: [PATCH] Order WelcomePageHandlerMapping at lower precedence This commit orders the `WelcomePageHandlerMapping` at `Ordered.LOWEST_PRECEDENCE -1` in order to give a chance to other mappings to handle the incoming requests. In this case, developers might provide a custom `ViewController` or custom `HandlerMapping` for the `"/"` path and we should not override that opinion. Closes gh-12335 --- .../web/servlet/WelcomePageHandlerMapping.java | 3 ++- .../web/servlet/WelcomePageHandlerMappingTests.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java index dad7d8b231d..368cc0187c0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; import org.springframework.context.ApplicationContext; +import org.springframework.core.Ordered; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -74,7 +75,7 @@ final class WelcomePageHandlerMapping extends AbstractUrlHandlerMapping { ParameterizableViewController controller = new ParameterizableViewController(); controller.setViewName(viewName); setRootHandler(controller); - setOrder(0); + setOrder(Ordered.LOWEST_PRECEDENCE - 1); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java index 8d2f7442c11..b7b4421da9f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java @@ -35,6 +35,7 @@ import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; @@ -62,6 +63,15 @@ public class WelcomePageHandlerMappingTests { .withUserConfiguration(HandlerMappingConfiguration.class).withConfiguration( AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class)); + @Test + public void isOrderedAtLowPriority() { + this.contextRunner.withUserConfiguration(StaticResourceConfiguration.class) + .run((context) -> { + WelcomePageHandlerMapping handler = context.getBean(WelcomePageHandlerMapping.class); + assertThat(handler.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1); + }); + } + @Test public void handlesRequestForStaticPageThatAcceptsTextHtml() { this.contextRunner.withUserConfiguration(StaticResourceConfiguration.class)