Skip to content

Commit d546ea5

Browse files
committed
Merge pull request #31769 from vpavic
* pr/31769: Add support for customizing WebJars resource handler path pattern Closes gh-31769
2 parents ec63a98 + 94f42d1 commit d546ea5

File tree

8 files changed

+63
-6
lines changed

8 files changed

+63
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
189189
logger.debug("Default resource handling disabled");
190190
return;
191191
}
192-
if (!registry.hasMappingForPattern("/webjars/**")) {
193-
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
192+
String webjarsPathPattern = this.webFluxProperties.getWebjarsPathPattern();
193+
if (!registry.hasMappingForPattern(webjarsPathPattern)) {
194+
ResourceHandlerRegistration registration = registry.addResourceHandler(webjarsPathPattern)
194195
.addResourceLocations("classpath:/META-INF/resources/webjars/");
195196
configureResourceCaching(registration);
196197
customizeResourceHandlerRegistration(registration);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* {@link ConfigurationProperties properties} for Spring WebFlux.
2424
*
2525
* @author Brian Clozel
26+
* @author Vedran Pavic
2627
* @since 2.0.0
2728
*/
2829
@ConfigurationProperties(prefix = "spring.webflux")
@@ -40,6 +41,11 @@ public class WebFluxProperties {
4041
*/
4142
private String staticPathPattern = "/**";
4243

44+
/**
45+
* Path pattern used for WebJar assets.
46+
*/
47+
private String webjarsPathPattern = "/webjars/**";
48+
4349
public String getBasePath() {
4450
return this.basePath;
4551
}
@@ -76,6 +82,14 @@ public void setStaticPathPattern(String staticPathPattern) {
7682
this.staticPathPattern = staticPathPattern;
7783
}
7884

85+
public String getWebjarsPathPattern() {
86+
return this.webjarsPathPattern;
87+
}
88+
89+
public void setWebjarsPathPattern(String webjarsPathPattern) {
90+
this.webjarsPathPattern = webjarsPathPattern;
91+
}
92+
7993
public static class Format {
8094

8195
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
329329
logger.debug("Default resource handling disabled");
330330
return;
331331
}
332-
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
332+
addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(),
333+
"classpath:/META-INF/resources/webjars/");
333334
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
334335
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
335336
if (this.servletContext != null) {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
* @author Stephane Nicoll
3636
* @author Eddú Meléndez
3737
* @author Brian Clozel
38+
* @author Vedran Pavic
3839
* @since 2.0.0
3940
*/
4041
@ConfigurationProperties(prefix = "spring.mvc")
@@ -91,6 +92,11 @@ public class WebMvcProperties {
9192
*/
9293
private String staticPathPattern = "/**";
9394

95+
/**
96+
* Path pattern used for WebJar assets.
97+
*/
98+
private String webjarsPathPattern = "/webjars/**";
99+
94100
private final Async async = new Async();
95101

96102
private final Servlet servlet = new Servlet();
@@ -188,6 +194,14 @@ public void setStaticPathPattern(String staticPathPattern) {
188194
this.staticPathPattern = staticPathPattern;
189195
}
190196

197+
public String getWebjarsPathPattern() {
198+
return this.webjarsPathPattern;
199+
}
200+
201+
public void setWebjarsPathPattern(String webjarsPathPattern) {
202+
this.webjarsPathPattern = webjarsPathPattern;
203+
}
204+
191205
public Async getAsync() {
192206
return this.async;
193207
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
* @author Brian Clozel
110110
* @author Andy Wilkinson
111111
* @author Artsiom Yudovin
112+
* @author Vedran Pavic
112113
*/
113114
class WebFluxAutoConfigurationTests {
114115

@@ -186,6 +187,18 @@ void shouldMapResourcesToCustomPath() {
186187
});
187188
}
188189

190+
@Test
191+
void shouldMapWebjarsToCustomPath() {
192+
this.contextRunner.withPropertyValues("spring.webflux.webjars-path-pattern:/assets/**").run((context) -> {
193+
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
194+
assertThat(hm.getUrlMap().get("/assets/**")).isInstanceOf(ResourceWebHandler.class);
195+
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/assets/**");
196+
assertThat(webjarsHandler.getLocations()).hasSize(1);
197+
assertThat(webjarsHandler.getLocations().get(0))
198+
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
199+
});
200+
}
201+
189202
@Test
190203
void shouldNotMapResourcesWhenDisabled() {
191204
this.contextRunner.withPropertyValues("spring.web.resources.add-mappings:false")

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
* @author Kristine Jetzke
147147
* @author Artsiom Yudovin
148148
* @author Scott Frederick
149+
* @author Vedran Pavic
149150
*/
150151
class WebMvcAutoConfigurationTests {
151152

@@ -195,6 +196,17 @@ void customResourceHandlerMapping() {
195196
});
196197
}
197198

199+
@Test
200+
void customWebjarsHandlerMapping() {
201+
this.contextRunner.withPropertyValues("spring.mvc.webjars-path-pattern:/assets/**").run((context) -> {
202+
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
203+
assertThat(locations.get("/assets/**")).hasSize(1);
204+
assertThat(locations.get("/assets/**").get(0))
205+
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
206+
assertThat(getResourceResolvers(context, "/assets/**")).hasSize(1);
207+
});
208+
}
209+
198210
@Test
199211
void resourceHandlerMappingOverrideWebjars() {
200212
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {

spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/reactive.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ If you do so, the default welcome page detection switches to your custom locatio
8484
So, if there is an `index.html` in any of your locations on startup, it is the home page of the application.
8585

8686
In addition to the "`standard`" static resource locations listed earlier, a special case is made for https://www.webjars.org/[Webjars content].
87-
Any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
87+
By default, any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
88+
The path can be customized with the configprop:spring.webflux.webjars-path-pattern[] property.
8889

8990
TIP: Spring WebFlux applications do not strictly depend on the servlet API, so they cannot be deployed as war files and do not use the `src/main/webapp` directory.
9091

spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ You can also customize the static resource locations by using the configprop:spr
107107
The root servlet context path, `"/"`, is automatically added as a location as well.
108108

109109
In addition to the "`standard`" static resource locations mentioned earlier, a special case is made for https://www.webjars.org/[Webjars content].
110-
Any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
110+
By default, any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
111+
The path can be customized with the configprop:spring.mvc.webjars-path-pattern[] property.
111112

112113
TIP: Do not use the `src/main/webapp` directory if your application is packaged as a jar.
113114
Although this directory is a common standard, it works *only* with war packaging, and it is silently ignored by most build tools if you generate a jar.

0 commit comments

Comments
 (0)