From 066533de7eafe01884c947032e171fb27bf49085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Thu, 12 Nov 2015 11:11:04 -0500 Subject: [PATCH] Add `spring.mvc.static-path-pattern` property Add a `spring.mvc.static-path-pattern` property which can be used to configure the path pattern used to serve static resources. Fixes gh-4444 Closes gh-4448 --- .../autoconfigure/web/WebMvcAutoConfiguration.java | 6 ++++-- .../boot/autoconfigure/web/WebMvcProperties.java | 14 ++++++++++++++ .../web/WebMvcAutoConfigurationTests.java | 9 +++++++++ .../asciidoc/appendix-application-properties.adoc | 1 + 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 9d68ec43d9..4e7b3274fb 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -98,6 +98,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; * @author Dave Syer * @author Andy Wilkinson * @author Sébastien Deleuze + * @author Eddú Meléndez */ @Configuration @ConditionalOnWebApplication @@ -256,8 +257,9 @@ public class WebMvcAutoConfiguration { .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(cachePeriod)); } - if (!registry.hasMappingForPattern("/**")) { - registerResourceChain(registry.addResourceHandler("/**") + String staticPathPattern = this.mvcProperties.getStaticPathPattern(); + if (!registry.hasMappingForPattern(staticPathPattern)) { + registerResourceChain(registry.addResourceHandler(staticPathPattern) .addResourceLocations( this.resourceProperties.getStaticLocations()) .setCachePeriod(cachePeriod)); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java index d91fb65ff7..bf7e4b84ed 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java @@ -31,6 +31,7 @@ import org.springframework.validation.DefaultMessageCodesResolver; * @author Phillip Webb * @author Sébastien Deleuze * @author Stephane Nicoll + * @author Eddú Meléndez * @since 1.1 */ @ConfigurationProperties("spring.mvc") @@ -77,6 +78,11 @@ public class WebMvcProperties { */ private Map mediaTypes = new LinkedHashMap(); + /** + * Path that pattern used for static resources. + */ + private String staticPathPattern = "/**"; + private final Async async = new Async(); private final View view = new View(); @@ -147,6 +153,14 @@ public class WebMvcProperties { this.dispatchTraceRequest = dispatchTraceRequest; } + public String getStaticPathPattern() { + return this.staticPathPattern; + } + + public void setStaticPathPattern(String staticPathPattern) { + this.staticPathPattern = staticPathPattern; + } + public Async getAsync() { return this.async; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index f8bbbbee19..410d638e38 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -101,6 +101,7 @@ import static org.junit.Assert.assertTrue; * @author Andy Wilkinson * @author Stephane Nicoll * @author Brian Clozel + * @author Eddú Meléndez */ public class WebMvcAutoConfigurationTests { @@ -149,6 +150,14 @@ public class WebMvcAutoConfigurationTests { assertThat(getResourceTransformers("/**").size(), equalTo(0)); } + @Test + public void customResourceHandlerMapping() throws Exception { + load("spring.mvc.static-path-pattern:/static/**"); + Map> mappingLocations = getResourceMappingLocations(); + assertThat(mappingLocations.get("/static/**").size(), equalTo(5)); + assertThat(getResourceResolvers("/static/**").size(), equalTo(1)); + } + @Test public void resourceHandlerMappingOverrideWebjars() throws Exception { load(WebJars.class); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 484b63d1ac..eac5e95795 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -310,6 +310,7 @@ content into your application; rather pick only the properties that you need. spring.mvc.locale= # Locale to use. spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation. spring.mvc.message-codes-resolver-format= # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`. + spring.mvc.static-path-pattern=/** # Path that pattern used for static resources. spring.mvc.throw-exception-if-no-handler-found=false # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request. spring.mvc.view.prefix= # Spring MVC view prefix. spring.mvc.view.suffix= # Spring MVC view suffix.