Skip to content

Commit 6a32955

Browse files
committed
Merge pull request #4448 from eddumelendez/gh-4444
* pr/4448: Add `spring.mvc.static-path-pattern` property
2 parents 854dadc + 066533d commit 6a32955

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
* @author Dave Syer
9999
* @author Andy Wilkinson
100100
* @author Sébastien Deleuze
101+
* @author Eddú Meléndez
101102
*/
102103
@Configuration
103104
@ConditionalOnWebApplication
@@ -256,8 +257,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
256257
.addResourceLocations("classpath:/META-INF/resources/webjars/")
257258
.setCachePeriod(cachePeriod));
258259
}
259-
if (!registry.hasMappingForPattern("/**")) {
260-
registerResourceChain(registry.addResourceHandler("/**")
260+
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
261+
if (!registry.hasMappingForPattern(staticPathPattern)) {
262+
registerResourceChain(registry.addResourceHandler(staticPathPattern)
261263
.addResourceLocations(
262264
this.resourceProperties.getStaticLocations())
263265
.setCachePeriod(cachePeriod));

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Phillip Webb
3232
* @author Sébastien Deleuze
3333
* @author Stephane Nicoll
34+
* @author Eddú Meléndez
3435
* @since 1.1
3536
*/
3637
@ConfigurationProperties("spring.mvc")
@@ -77,6 +78,11 @@ public class WebMvcProperties {
7778
*/
7879
private Map<String, MediaType> mediaTypes = new LinkedHashMap<String, MediaType>();
7980

81+
/**
82+
* Path that pattern used for static resources.
83+
*/
84+
private String staticPathPattern = "/**";
85+
8086
private final Async async = new Async();
8187

8288
private final View view = new View();
@@ -147,6 +153,14 @@ public void setDispatchTraceRequest(boolean dispatchTraceRequest) {
147153
this.dispatchTraceRequest = dispatchTraceRequest;
148154
}
149155

156+
public String getStaticPathPattern() {
157+
return this.staticPathPattern;
158+
}
159+
160+
public void setStaticPathPattern(String staticPathPattern) {
161+
this.staticPathPattern = staticPathPattern;
162+
}
163+
150164
public Async getAsync() {
151165
return this.async;
152166
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
* @author Andy Wilkinson
102102
* @author Stephane Nicoll
103103
* @author Brian Clozel
104+
* @author Eddú Meléndez
104105
*/
105106
public class WebMvcAutoConfigurationTests {
106107

@@ -149,6 +150,14 @@ public void resourceHandlerMapping() throws Exception {
149150
assertThat(getResourceTransformers("/**").size(), equalTo(0));
150151
}
151152

153+
@Test
154+
public void customResourceHandlerMapping() throws Exception {
155+
load("spring.mvc.static-path-pattern:/static/**");
156+
Map<String, List<Resource>> mappingLocations = getResourceMappingLocations();
157+
assertThat(mappingLocations.get("/static/**").size(), equalTo(5));
158+
assertThat(getResourceResolvers("/static/**").size(), equalTo(1));
159+
}
160+
152161
@Test
153162
public void resourceHandlerMappingOverrideWebjars() throws Exception {
154163
load(WebJars.class);

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ content into your application; rather pick only the properties that you need.
310310
spring.mvc.locale= # Locale to use.
311311
spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation.
312312
spring.mvc.message-codes-resolver-format= # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`.
313+
spring.mvc.static-path-pattern=/** # Path that pattern used for static resources.
313314
spring.mvc.throw-exception-if-no-handler-found=false # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request.
314315
spring.mvc.view.prefix= # Spring MVC view prefix.
315316
spring.mvc.view.suffix= # Spring MVC view suffix.

0 commit comments

Comments
 (0)