Skip to content

Commit b32a38a

Browse files
committed
Work around unwanted static resource warnings from WebFlux and MVC
See gh-28223
1 parent bad37f9 commit b32a38a

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -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;
@@ -49,6 +51,7 @@
4951
import org.springframework.context.annotation.Configuration;
5052
import org.springframework.context.annotation.Import;
5153
import org.springframework.core.Ordered;
54+
import org.springframework.core.io.ResourceLoader;
5255
import org.springframework.format.FormatterRegistry;
5356
import org.springframework.format.support.FormattingConversionService;
5457
import org.springframework.http.codec.ServerCodecConfigurer;
@@ -151,12 +154,14 @@ public static class WebFluxConfig implements WebFluxConfigurer {
151154

152155
private final ObjectProvider<ViewResolver> viewResolvers;
153156

157+
private final ResourceLoader resourceLoader;
158+
154159
public WebFluxConfig(org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
155160
WebProperties webProperties, WebFluxProperties webFluxProperties, ListableBeanFactory beanFactory,
156161
ObjectProvider<HandlerMethodArgumentResolver> resolvers,
157162
ObjectProvider<CodecCustomizer> codecCustomizers,
158163
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizer,
159-
ObjectProvider<ViewResolver> viewResolvers) {
164+
ObjectProvider<ViewResolver> viewResolvers, ResourceLoader resourceLoader) {
160165
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
161166
: webProperties.getResources();
162167
this.webFluxProperties = webFluxProperties;
@@ -165,6 +170,7 @@ public WebFluxConfig(org.springframework.boot.autoconfigure.web.ResourceProperti
165170
this.codecCustomizers = codecCustomizers;
166171
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizer.getIfAvailable();
167172
this.viewResolvers = viewResolvers;
173+
this.resourceLoader = resourceLoader;
168174
}
169175

170176
@Override
@@ -184,17 +190,28 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
184190
return;
185191
}
186192
if (!registry.hasMappingForPattern("/webjars/**")) {
187-
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
188-
.addResourceLocations("classpath:/META-INF/resources/webjars/");
189-
configureResourceCaching(registration);
190-
customizeResourceHandlerRegistration(registration);
193+
String webjarsLocation = "classpath:/META-INF/resources/webjars/";
194+
if (this.resourceLoader.getResource(webjarsLocation).exists()) {
195+
ResourceHandlerRegistration registration = registry.addResourceHandler("/webjars/**")
196+
.addResourceLocations(webjarsLocation);
197+
configureResourceCaching(registration);
198+
customizeResourceHandlerRegistration(registration);
199+
}
191200
}
192201
String staticPathPattern = this.webFluxProperties.getStaticPathPattern();
193202
if (!registry.hasMappingForPattern(staticPathPattern)) {
194-
ResourceHandlerRegistration registration = registry.addResourceHandler(staticPathPattern)
195-
.addResourceLocations(this.resourceProperties.getStaticLocations());
196-
configureResourceCaching(registration);
197-
customizeResourceHandlerRegistration(registration);
203+
List<String> foundLocations = new ArrayList<>();
204+
for (String staticLocation : this.resourceProperties.getStaticLocations()) {
205+
if (this.resourceLoader.getResource(staticLocation).exists()) {
206+
foundLocations.add(staticLocation);
207+
}
208+
}
209+
if (!foundLocations.isEmpty()) {
210+
ResourceHandlerRegistration registration = registry.addResourceHandler(staticPathPattern)
211+
.addResourceLocations(foundLocations.toArray(new String[0]));
212+
configureResourceCaching(registration);
213+
customizeResourceHandlerRegistration(registration);
214+
}
198215
}
199216
}
200217

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
@@ -150,9 +150,10 @@ void shouldRegisterResourceHandlerMapping() {
150150
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
151151
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
152152
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
153-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
154-
assertThat(staticHandler.getLocations()).hasSize(1);
155-
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [public/]");
153+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
154+
assertThat(staticHandler.getLocations()).hasSize(2);
155+
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [META-INF/resources/]");
156+
assertThat(staticHandler.getLocations().get(1)).hasToString("class path resource [public/]");
156157
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
157158
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
158159
assertThat(webjarsHandler).extracting("locationValues").asList()
@@ -166,7 +167,7 @@ void shouldMapResourcesToCustomPath() {
166167
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
167168
assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
168169
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
169-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
170+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
170171
});
171172
}
172173

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

Whitespace-only changes.

0 commit comments

Comments
 (0)