From 3b5fa5a6a324a2f6d5ce41052127f15652a5b1bc Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 23 Jul 2015 14:22:52 +0200 Subject: [PATCH] Clean server.context-path if necessary While the doc states that the default value is '/', setting that value explicitly will lead to an error since we enforce that the default root is the empty string. Changing the doc will probably be more confusing than anything else so we're now cleaning the user's provided value if necessary Closes gh-3554 --- .../boot/autoconfigure/web/ServerProperties.java | 9 ++++++++- .../autoconfigure/web/ServerPropertiesTests.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 7fdab453c7..6d034d04ce 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -139,7 +139,14 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord } public void setContextPath(String contextPath) { - this.contextPath = contextPath; + this.contextPath = cleanContextPath(contextPath); + } + + private String cleanContextPath(String contextPath) { + if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { + return contextPath.substring(0, contextPath.length() - 1); + } + return contextPath; } public String getDisplayName() { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index c34ac51b3b..bab6847c86 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -41,6 +41,7 @@ import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletCont import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -123,6 +124,20 @@ public class ServerPropertiesTests { .getInternalProxies()); } + @Test + public void testTrailingSlashOfContextPathIsRemoved() { + new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( + Collections.singletonMap("server.contextPath", "/foo/"))); + assertThat(this.properties.getContextPath(), equalTo("/foo")); + } + + @Test + public void testSlashOfContextPathIsDefaultValue() { + new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( + Collections.singletonMap("server.contextPath", "/"))); + assertThat(this.properties.getContextPath(), equalTo("")); + } + @Test public void testCustomizeTomcat() throws Exception { ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);