diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index b4f1ce5ba0b3..caface272288 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -538,6 +538,9 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon // Supported methods and required session checkRequest(request); + // Apply cache settings, if any + prepareResponse(response); + // Header phase String eTagValue = (this.getEtagGenerator() != null) ? this.getEtagGenerator().apply(resource) : null; long lastModified = (this.isUseLastModified()) ? resource.lastModified() : -1; @@ -546,9 +549,6 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon return; } - // Apply cache settings, if any - prepareResponse(response); - // Check the media type for the resource MediaType mediaType = getMediaType(request, resource); setHeaders(response, resource, mediaType); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index d796b37a4fc5..c33e4c4d0d27 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -465,11 +465,13 @@ void configureVersionResourceResolver() throws Exception { @Test void shouldRespondWithNotModifiedWhenModifiedSince() throws Exception { + this.handler.setCacheSeconds(3600); this.handler.afterPropertiesSet(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css")); this.handler.handleRequest(this.request, this.response); assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED); + assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600"); } @Test @@ -484,12 +486,14 @@ void shouldRespondWithModifiedResource() throws Exception { @Test void shouldRespondWithNotModifiedWhenEtag() throws Exception { + this.handler.setCacheSeconds(3600); this.handler.setEtagGenerator(resource -> "testEtag"); this.handler.afterPropertiesSet(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.addHeader("If-None-Match", "\"testEtag\""); this.handler.handleRequest(this.request, this.response); assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_MODIFIED); + assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600"); } @Test