Merge pull request #31769 from vpavic
* pr/31769: Add support for customizing WebJars resource handler path pattern Closes gh-31769
This commit is contained in:
commit
d546ea515a
|
|
@ -189,8 +189,9 @@ public class WebFluxAutoConfiguration {
|
|||
logger.debug("Default resource handling disabled");
|
||||
return;
|
||||
}
|
||||
if (!registry.hasMappingForPattern("/webjars/**")) {
|
||||
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
|
||||
String webjarsPathPattern = this.webFluxProperties.getWebjarsPathPattern();
|
||||
if (!registry.hasMappingForPattern(webjarsPathPattern)) {
|
||||
ResourceHandlerRegistration registration = registry.addResourceHandler(webjarsPathPattern)
|
||||
.addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
configureResourceCaching(registration);
|
||||
customizeResourceHandlerRegistration(registration);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import org.springframework.util.StringUtils;
|
|||
* {@link ConfigurationProperties properties} for Spring WebFlux.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Vedran Pavic
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.webflux")
|
||||
|
|
@ -40,6 +41,11 @@ public class WebFluxProperties {
|
|||
*/
|
||||
private String staticPathPattern = "/**";
|
||||
|
||||
/**
|
||||
* Path pattern used for WebJar assets.
|
||||
*/
|
||||
private String webjarsPathPattern = "/webjars/**";
|
||||
|
||||
public String getBasePath() {
|
||||
return this.basePath;
|
||||
}
|
||||
|
|
@ -76,6 +82,14 @@ public class WebFluxProperties {
|
|||
this.staticPathPattern = staticPathPattern;
|
||||
}
|
||||
|
||||
public String getWebjarsPathPattern() {
|
||||
return this.webjarsPathPattern;
|
||||
}
|
||||
|
||||
public void setWebjarsPathPattern(String webjarsPathPattern) {
|
||||
this.webjarsPathPattern = webjarsPathPattern;
|
||||
}
|
||||
|
||||
public static class Format {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -329,7 +329,8 @@ public class WebMvcAutoConfiguration {
|
|||
logger.debug("Default resource handling disabled");
|
||||
return;
|
||||
}
|
||||
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
|
||||
addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(),
|
||||
"classpath:/META-INF/resources/webjars/");
|
||||
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
|
||||
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
|
||||
if (this.servletContext != null) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -35,6 +35,7 @@ import org.springframework.validation.DefaultMessageCodesResolver;
|
|||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @author Brian Clozel
|
||||
* @author Vedran Pavic
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.mvc")
|
||||
|
|
@ -91,6 +92,11 @@ public class WebMvcProperties {
|
|||
*/
|
||||
private String staticPathPattern = "/**";
|
||||
|
||||
/**
|
||||
* Path pattern used for WebJar assets.
|
||||
*/
|
||||
private String webjarsPathPattern = "/webjars/**";
|
||||
|
||||
private final Async async = new Async();
|
||||
|
||||
private final Servlet servlet = new Servlet();
|
||||
|
|
@ -188,6 +194,14 @@ public class WebMvcProperties {
|
|||
this.staticPathPattern = staticPathPattern;
|
||||
}
|
||||
|
||||
public String getWebjarsPathPattern() {
|
||||
return this.webjarsPathPattern;
|
||||
}
|
||||
|
||||
public void setWebjarsPathPattern(String webjarsPathPattern) {
|
||||
this.webjarsPathPattern = webjarsPathPattern;
|
||||
}
|
||||
|
||||
public Async getAsync() {
|
||||
return this.async;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ import static org.mockito.Mockito.mock;
|
|||
* @author Brian Clozel
|
||||
* @author Andy Wilkinson
|
||||
* @author Artsiom Yudovin
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class WebFluxAutoConfigurationTests {
|
||||
|
||||
|
|
@ -186,6 +187,18 @@ class WebFluxAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMapWebjarsToCustomPath() {
|
||||
this.contextRunner.withPropertyValues("spring.webflux.webjars-path-pattern:/assets/**").run((context) -> {
|
||||
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
|
||||
assertThat(hm.getUrlMap().get("/assets/**")).isInstanceOf(ResourceWebHandler.class);
|
||||
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/assets/**");
|
||||
assertThat(webjarsHandler.getLocations()).hasSize(1);
|
||||
assertThat(webjarsHandler.getLocations().get(0))
|
||||
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotMapResourcesWhenDisabled() {
|
||||
this.contextRunner.withPropertyValues("spring.web.resources.add-mappings:false")
|
||||
|
|
|
|||
|
|
@ -146,6 +146,7 @@ import static org.mockito.Mockito.mock;
|
|||
* @author Kristine Jetzke
|
||||
* @author Artsiom Yudovin
|
||||
* @author Scott Frederick
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class WebMvcAutoConfigurationTests {
|
||||
|
||||
|
|
@ -195,6 +196,17 @@ class WebMvcAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customWebjarsHandlerMapping() {
|
||||
this.contextRunner.withPropertyValues("spring.mvc.webjars-path-pattern:/assets/**").run((context) -> {
|
||||
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
|
||||
assertThat(locations.get("/assets/**")).hasSize(1);
|
||||
assertThat(locations.get("/assets/**").get(0))
|
||||
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
|
||||
assertThat(getResourceResolvers(context, "/assets/**")).hasSize(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceHandlerMappingOverrideWebjars() {
|
||||
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {
|
||||
|
|
|
|||
|
|
@ -84,7 +84,8 @@ If you do so, the default welcome page detection switches to your custom locatio
|
|||
So, if there is an `index.html` in any of your locations on startup, it is the home page of the application.
|
||||
|
||||
In addition to the "`standard`" static resource locations listed earlier, a special case is made for https://www.webjars.org/[Webjars content].
|
||||
Any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
|
||||
By default, any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
|
||||
The path can be customized with the configprop:spring.webflux.webjars-path-pattern[] property.
|
||||
|
||||
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.
|
||||
|
||||
|
|
|
|||
|
|
@ -107,7 +107,8 @@ You can also customize the static resource locations by using the configprop:spr
|
|||
The root servlet context path, `"/"`, is automatically added as a location as well.
|
||||
|
||||
In addition to the "`standard`" static resource locations mentioned earlier, a special case is made for https://www.webjars.org/[Webjars content].
|
||||
Any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
|
||||
By default, any resources with a path in `+/webjars/**+` are served from jar files if they are packaged in the Webjars format.
|
||||
The path can be customized with the configprop:spring.mvc.webjars-path-pattern[] property.
|
||||
|
||||
TIP: Do not use the `src/main/webapp` directory if your application is packaged as a jar.
|
||||
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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue