diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java index 2eb77ddb1c..8c6477227c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java @@ -148,17 +148,11 @@ public class WebFluxAutoConfiguration { logger.debug("Default resource handling disabled"); return; } - Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); - ResourceProperties.Cache.Cachecontrol cacheControl = this.resourceProperties - .getCache().getCachecontrol(); if (!registry.hasMappingForPattern("/webjars/**")) { ResourceHandlerRegistration registration = registry .addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); - if (cachePeriod != null && cacheControl.getMaxAge() == null) { - cacheControl.setMaxAge(cachePeriod); - } - registration.setCacheControl(cacheControl.toHttpCacheControl()); + configureResourceCaching(registration); customizeResourceHandlerRegistration(registration); } String staticPathPattern = this.webFluxProperties.getStaticPathPattern(); @@ -166,14 +160,21 @@ public class WebFluxAutoConfiguration { ResourceHandlerRegistration registration = registry .addResourceHandler(staticPathPattern).addResourceLocations( this.resourceProperties.getStaticLocations()); - if (cachePeriod != null && cacheControl.getMaxAge() == null) { - cacheControl.setMaxAge(cachePeriod); - } - registration.setCacheControl(cacheControl.toHttpCacheControl()); + configureResourceCaching(registration); customizeResourceHandlerRegistration(registration); } } + private void configureResourceCaching(ResourceHandlerRegistration registration) { + Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); + ResourceProperties.Cache.Cachecontrol cacheControl = this.resourceProperties + .getCache().getCachecontrol(); + if (cachePeriod != null && cacheControl.getMaxAge() == null) { + cacheControl.setMaxAge(cachePeriod); + } + registration.setCacheControl(cacheControl.toHttpCacheControl()); + } + @Override public void configureViewResolvers(ViewResolverRegistry registry) { this.viewResolvers.orderedStream().forEach(registry::viewResolver); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java index 7677434c79..de5672ecc7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java @@ -31,10 +31,10 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.autoconfigure.validation.ValidatorAdapter; -import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -415,7 +415,17 @@ public class WebFluxAutoConfigurationTests { @Test public void cachePeriod() { this.contextRunner.withPropertyValues("spring.resources.cache.period:5") - .run(this::assertCachePeriod); + .run((context) -> { + Map handlerMap = getHandlerMap(context); + assertThat(handlerMap).hasSize(2); + for (Object handler : handlerMap.values()) { + if (handler instanceof ResourceWebHandler) { + assertThat(((ResourceWebHandler) handler).getCacheControl()) + .isEqualToComparingFieldByField( + CacheControl.maxAge(5, TimeUnit.SECONDS)); + } + } + }); } @Test @@ -423,42 +433,29 @@ public class WebFluxAutoConfigurationTests { this.contextRunner .withPropertyValues("spring.resources.cache.cachecontrol.max-age:5", "spring.resources.cache.cachecontrol.proxy-revalidate:true") - .run(this::assertCacheControl); + .run((context) -> { + Map handlerMap = getHandlerMap(context); + assertThat(handlerMap).hasSize(2); + for (Object handler : handlerMap.values()) { + if (handler instanceof ResourceWebHandler) { + assertThat(((ResourceWebHandler) handler).getCacheControl()) + .isEqualToComparingFieldByField( + CacheControl.maxAge(5, TimeUnit.SECONDS) + .proxyRevalidate()); + } + } + }); } - private void assertCachePeriod(AssertableReactiveWebApplicationContext context) { - Map handlerMap = getHandlerMap( - context.getBean("resourceHandlerMapping", HandlerMapping.class)); - assertThat(handlerMap).hasSize(2); - for (Object handler : handlerMap.values()) { - if (handler instanceof ResourceWebHandler) { - assertThat(((ResourceWebHandler) handler).getCacheControl()) - .isEqualToComparingFieldByField( - CacheControl.maxAge(5, TimeUnit.SECONDS)); - } - } - } - - private Map getHandlerMap(HandlerMapping mapping) { + private Map getHandlerMap(ApplicationContext context) { + HandlerMapping mapping = context.getBean("resourceHandlerMapping", + HandlerMapping.class); if (mapping instanceof SimpleUrlHandlerMapping) { return ((SimpleUrlHandlerMapping) mapping).getHandlerMap(); } return Collections.emptyMap(); } - private void assertCacheControl(AssertableReactiveWebApplicationContext context) { - Map handlerMap = getHandlerMap( - context.getBean("resourceHandlerMapping", HandlerMapping.class)); - assertThat(handlerMap).hasSize(2); - for (Object handler : handlerMap.values()) { - if (handler instanceof ResourceWebHandler) { - assertThat(((ResourceWebHandler) handler).getCacheControl()) - .isEqualToComparingFieldByField(CacheControl - .maxAge(5, TimeUnit.SECONDS).proxyRevalidate()); - } - } - } - @Configuration protected static class CustomArgumentResolvers {