Skip to content

Commit 196013f

Browse files
committed
Merge branch '2.4.x' into 2.5.x
See gh-28241
2 parents 8b0c563 + b32a38a commit 196013f

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.autoconfigure.web.reactive;
1818

1919
import java.time.Duration;
20+
import java.util.ArrayList;
21+
import java.util.List;
2022

2123
import org.apache.commons.logging.Log;
2224
import org.apache.commons.logging.LogFactory;
@@ -50,6 +52,7 @@
5052
import org.springframework.context.annotation.Import;
5153
import org.springframework.core.Ordered;
5254
import org.springframework.core.annotation.Order;
55+
import org.springframework.core.io.ResourceLoader;
5356
import org.springframework.format.FormatterRegistry;
5457
import org.springframework.format.support.FormattingConversionService;
5558
import org.springframework.http.codec.ServerCodecConfigurer;
@@ -156,12 +159,14 @@ public static class WebFluxConfig implements WebFluxConfigurer {
156159

157160
private final ObjectProvider<ViewResolver> viewResolvers;
158161

162+
private final ResourceLoader resourceLoader;
163+
159164
public WebFluxConfig(org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
160165
WebProperties webProperties, WebFluxProperties webFluxProperties, ListableBeanFactory beanFactory,
161166
ObjectProvider<HandlerMethodArgumentResolver> resolvers,
162167
ObjectProvider<CodecCustomizer> codecCustomizers,
163168
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizer,
164-
ObjectProvider<ViewResolver> viewResolvers) {
169+
ObjectProvider<ViewResolver> viewResolvers, ResourceLoader resourceLoader) {
165170
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
166171
: webProperties.getResources();
167172
this.webFluxProperties = webFluxProperties;
@@ -170,6 +175,7 @@ public WebFluxConfig(org.springframework.boot.autoconfigure.web.ResourceProperti
170175
this.codecCustomizers = codecCustomizers;
171176
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizer.getIfAvailable();
172177
this.viewResolvers = viewResolvers;
178+
this.resourceLoader = resourceLoader;
173179
}
174180

175181
@Override
@@ -189,17 +195,28 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
189195
return;
190196
}
191197
if (!registry.hasMappingForPattern("/webjars/**")) {
192-
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
193-
.addResourceLocations("classpath:/META-INF/resources/webjars/");
194-
configureResourceCaching(registration);
195-
customizeResourceHandlerRegistration(registration);
198+
String webjarsLocation = "classpath:/META-INF/resources/webjars/";
199+
if (this.resourceLoader.getResource(webjarsLocation).exists()) {
200+
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
201+
.addResourceLocations(webjarsLocation);
202+
configureResourceCaching(registration);
203+
customizeResourceHandlerRegistration(registration);
204+
}
196205
}
197206
String staticPathPattern = this.webFluxProperties.getStaticPathPattern();
198207
if (!registry.hasMappingForPattern(staticPathPattern)) {
199-
ResourceHandlerRegistration registration = registry.addResourceHandler(staticPathPattern)
200-
.addResourceLocations(this.resourceProperties.getStaticLocations());
201-
configureResourceCaching(registration);
202-
customizeResourceHandlerRegistration(registration);
208+
List<String> foundLocations = new ArrayList<>();
209+
for (String staticLocation : this.resourceProperties.getStaticLocations()) {
210+
if (this.resourceLoader.getResource(staticLocation).exists()) {
211+
foundLocations.add(staticLocation);
212+
}
213+
}
214+
if (!foundLocations.isEmpty()) {
215+
ResourceHandlerRegistration registration = registry.addResourceHandler(staticPathPattern)
216+
.addResourceLocations(foundLocations.toArray(new String[0]));
217+
configureResourceCaching(registration);
218+
customizeResourceHandlerRegistration(registration);
219+
}
203220
}
204221
}
205222

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer,
201201

202202
private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
203203

204+
private final ResourceLoader resourceLoader;
205+
204206
private ServletContext servletContext;
205207

206208
public WebMvcAutoConfigurationAdapter(
@@ -209,7 +211,7 @@ public WebMvcAutoConfigurationAdapter(
209211
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
210212
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
211213
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
212-
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
214+
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations, ResourceLoader resourceLoader) {
213215
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
214216
: webProperties.getResources();
215217
this.mvcProperties = mvcProperties;
@@ -218,6 +220,7 @@ public WebMvcAutoConfigurationAdapter(
218220
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
219221
this.dispatcherServletPath = dispatcherServletPath;
220222
this.servletRegistrations = servletRegistrations;
223+
this.resourceLoader = resourceLoader;
221224
this.mvcProperties.checkConfiguration();
222225
}
223226

@@ -334,7 +337,11 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
334337
logger.debug("Default resource handling disabled");
335338
return;
336339
}
337-
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
340+
Resource webjarsLocationResource = this.resourceLoader
341+
.getResource("classpath:/META-INF/resources/webjars/");
342+
if (webjarsLocationResource.exists()) {
343+
addResourceHandler(registry, "/webjars/**", webjarsLocationResource);
344+
}
338345
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
339346
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
340347
if (this.servletContext != null) {
@@ -344,7 +351,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
344351
});
345352
}
346353

347-
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
354+
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, Resource... locations) {
348355
addResourceHandler(registry, pattern, (registration) -> registration.addResourceLocations(locations));
349356
}
350357

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ void shouldRegisterResourceHandlerMapping() {
157157
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
158158
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
159159
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
160-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
161-
assertThat(staticHandler.getLocations()).hasSize(1);
162-
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [public/]");
160+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
161+
assertThat(staticHandler.getLocations()).hasSize(2);
162+
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [META-INF/resources/]");
163+
assertThat(staticHandler.getLocations().get(1)).hasToString("class path resource [public/]");
163164
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
164165
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
165166
assertThat(webjarsHandler).extracting("locationValues").asList()
@@ -173,7 +174,7 @@ void shouldMapResourcesToCustomPath() {
173174
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
174175
assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
175176
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
176-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
177+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
177178
});
178179
}
179180

spring-boot-project/spring-boot-autoconfigure/src/test/resources/META-INF/resources/webjars/webjar

Whitespace-only changes.

0 commit comments

Comments
 (0)