diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java index e32a430a578..c97d33e9723 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcProperties.java @@ -242,6 +242,7 @@ public class WebMvcProperties { public void setPath(String path) { Assert.notNull(path, "Path must not be null"); + Assert.isTrue(!path.contains("*"), "Path must not contain wildcards"); this.path = path; } @@ -257,9 +258,6 @@ public class WebMvcProperties { if (this.path.equals("") || this.path.equals("/")) { return "/"; } - if (this.path.contains("*")) { - return this.path; - } if (this.path.endsWith("/")) { return this.path + "*"; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcPropertiesTests.java index dbb637d27e1..d125cb07141 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcPropertiesTests.java @@ -20,13 +20,16 @@ import java.util.Collections; import java.util.Map; import org.junit.Test; +import org.testcontainers.shaded.com.google.common.base.Throwables; +import org.springframework.boot.context.properties.bind.BindException; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for {@link WebMvcProperties}. @@ -38,19 +41,28 @@ public class WebMvcPropertiesTests { private final WebMvcProperties properties = new WebMvcProperties(); @Test - public void testServletPathAsMapping() { - bind("spring.mvc.servlet.path", "/foo/*"); + public void servletPathWhenEndsWithSlashHasValidMappingAndPrefix() { + bind("spring.mvc.servlet.path", "/foo/"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo"); } @Test - public void testServletPathAsPrefix() { + public void servletPathWhenDoesNotEndWithSlashHasValidMappingAndPrefix() { bind("spring.mvc.servlet.path", "/foo"); assertThat(this.properties.getServlet().getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServlet().getServletPrefix()).isEqualTo("/foo"); } + @Test + public void servletPathWhenHasWildcardThrowsException() { + assertThatExceptionOfType(BindException.class) + .isThrownBy(() -> bind("spring.mvc.servlet.path", "/*")) + .withRootCauseInstanceOf(IllegalArgumentException.class) + .satisfies((ex) -> assertThat(Throwables.getRootCause(ex)) + .hasMessage("Path must not contain wildcards")); + } + private void bind(String name, String value) { bind(Collections.singletonMap(name, value)); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/RemappedErrorViewIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/RemappedErrorViewIntegrationTests.java index 6dd62658dc3..25b696480eb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/RemappedErrorViewIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/RemappedErrorViewIntegrationTests.java @@ -47,7 +47,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.mvc.servlet.path:/spring/*") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.mvc.servlet.path:/spring/") @DirtiesContext public class RemappedErrorViewIntegrationTests { diff --git a/spring-boot-samples/spring-boot-sample-atmosphere/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-atmosphere/src/main/resources/application.properties index ccbc4ee91cb..a1aa8c46917 100644 --- a/spring-boot-samples/spring-boot-sample-atmosphere/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-atmosphere/src/main/resources/application.properties @@ -1 +1 @@ -spring.mvc.servlet.path=/home/* +spring.mvc.servlet.path=/home/