Browse Source

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
pull/12428/merge
Brian Clozel 8 years ago
parent
commit
220f8cdca2
  1. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMapping.java
  2. 10
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePageHandlerMappingTests.java

3
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; @@ -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 { @@ -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

10
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; @@ -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 { @@ -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)

Loading…
Cancel
Save