diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java index c895e25528d..224ea92ca3d 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java @@ -702,7 +702,7 @@ public class TomcatServerProperties { /** * Time-to-live of the static resource cache. */ - private @Nullable Duration cacheTtl; + private Duration cacheTtl = Duration.ofSeconds(5); public boolean isAllowCaching() { return this.allowCaching; @@ -720,11 +720,11 @@ public class TomcatServerProperties { this.cacheMaxSize = cacheMaxSize; } - public @Nullable Duration getCacheTtl() { + public Duration getCacheTtl() { return this.cacheTtl; } - public void setCacheTtl(@Nullable Duration cacheTtl) { + public void setCacheTtl(Duration cacheTtl) { this.cacheTtl = cacheTtl; } diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java index b067915ae70..79ed471fd4a 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java @@ -380,12 +380,8 @@ public class TomcatWebServerFactoryCustomizer factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> { if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) { context.getResources().setCachingAllowed(resource.isAllowCaching()); - long cacheMaxSize = resource.getCacheMaxSize().toKilobytes(); - context.getResources().setCacheMaxSize(cacheMaxSize); - if (resource.getCacheTtl() != null) { - long ttl = resource.getCacheTtl().toMillis(); - context.getResources().setCacheTtl(ttl); - } + context.getResources().setCacheMaxSize(resource.getCacheMaxSize().toKilobytes()); + context.getResources().setCacheTtl(resource.getCacheTtl().toMillis()); } })); } diff --git a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java index 8f9e9446db3..2aec1268555 100644 --- a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java +++ b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.tomcat.autoconfigure; +import java.time.Duration; import java.util.Locale; import java.util.function.Consumer; @@ -331,6 +332,8 @@ class TomcatWebServerFactoryCustomizerTests { assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed()); assertThat(properties.getResource().getCacheMaxSize()) .isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize())); + assertThat(properties.getResource().getCacheTtl()) + .isEqualTo(Duration.ofMillis(context.getResources().getCacheTtl())); }); } @@ -356,7 +359,7 @@ class TomcatWebServerFactoryCustomizerTests { @Test void customStaticResourceCacheTtl() { - bind("server.tomcat.resource.cache-ttl=10000"); + bind("server.tomcat.resource.cache-ttl=10s"); customizeAndRunServer((server) -> { Tomcat tomcat = server.getTomcat(); Context context = (Context) tomcat.getHost().findChildren()[0];