Skip to content

Commit 3969e63

Browse files
committed
Merge branch '2.5.x'
See gh-28252
2 parents f01d086 + 196013f commit 3969e63

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
import java.util.function.Supplier;
2123

2224
import org.apache.commons.logging.Log;
@@ -51,6 +53,7 @@
5153
import org.springframework.context.annotation.Import;
5254
import org.springframework.core.Ordered;
5355
import org.springframework.core.annotation.Order;
56+
import org.springframework.core.io.ResourceLoader;
5457
import org.springframework.format.FormatterRegistry;
5558
import org.springframework.format.support.FormattingConversionService;
5659
import org.springframework.http.codec.ServerCodecConfigurer;
@@ -153,18 +156,21 @@ public static class WebFluxConfig implements WebFluxConfigurer {
153156

154157
private final ObjectProvider<ViewResolver> viewResolvers;
155158

159+
private final ResourceLoader resourceLoader;
160+
156161
public WebFluxConfig(WebProperties webProperties, WebFluxProperties webFluxProperties,
157162
ListableBeanFactory beanFactory, ObjectProvider<HandlerMethodArgumentResolver> resolvers,
158163
ObjectProvider<CodecCustomizer> codecCustomizers,
159164
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizer,
160-
ObjectProvider<ViewResolver> viewResolvers) {
165+
ObjectProvider<ViewResolver> viewResolvers, ResourceLoader resourceLoader) {
161166
this.resourceProperties = webProperties.getResources();
162167
this.webFluxProperties = webFluxProperties;
163168
this.beanFactory = beanFactory;
164169
this.argumentResolvers = resolvers;
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
@@ -204,20 +204,23 @@ public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer,
204204

205205
private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
206206

207+
private final ResourceLoader resourceLoader;
208+
207209
private ServletContext servletContext;
208210

209211
public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
210212
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
211213
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
212214
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
213-
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
215+
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations, ResourceLoader resourceLoader) {
214216
this.resourceProperties = webProperties.getResources();
215217
this.mvcProperties = mvcProperties;
216218
this.beanFactory = beanFactory;
217219
this.messageConvertersProvider = messageConvertersProvider;
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
@@ -160,9 +160,10 @@ void shouldRegisterResourceHandlerMapping() {
160160
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
161161
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
162162
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
163-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
164-
assertThat(staticHandler.getLocations()).hasSize(1);
165-
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [public/]");
163+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
164+
assertThat(staticHandler.getLocations()).hasSize(2);
165+
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [META-INF/resources/]");
166+
assertThat(staticHandler.getLocations().get(1)).hasToString("class path resource [public/]");
166167
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
167168
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
168169
assertThat(webjarsHandler).extracting("locationValues").asList()
@@ -176,7 +177,7 @@ void shouldMapResourcesToCustomPath() {
176177
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
177178
assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
178179
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
179-
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
180+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(2);
180181
});
181182
}
182183

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

Whitespace-only changes.

0 commit comments

Comments
 (0)