You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ResponseEntity CacheControl ignored / extended by RequestMappingHandlerAdapter [SPR-13867] #18440
Hi
I think there might a bug with some new implementation inside RequestMappingHandlerAdapter resulting in no-cache, no-store headers even when using an explicit ResponseEntity.
I would expect that this code inside the RequestMappingHandlerAdapter should not be called in such a case when a ResponseEntity is the outcome of a RequestMapping
@OverrideprotectedModelAndViewhandleInternal(HttpServletRequestrequest,
HttpServletResponseresponse, HandlerMethodhandlerMethod) throwsException {
checkRequest(request);
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
}
else {
prepareResponse(response);
}
.
.
.
}
FROMWebContentGenerator/** * Prepare the given response according to the settings of this generator. * Applies the number of cache seconds specified for this generator. * @param response current HTTP response * @since 4.2 */protectedfinalvoidprepareResponse(HttpServletResponseresponse) {
if (this.cacheControl != null) {
applyCacheControl(response, this.cacheControl);
}
else {
applyCacheSeconds(response, this.cacheSeconds);
}
}
Thanks for this report!
This is now fixed both in master and 4.2.x, meaning you can test this fix with 4.2.5.BUILD-SNAPSHOT or 4.3.0.BUILD-SNAPSHOT in a few minutes.
This is a two-part fix:
first, the "Cache-Control" processing done by the RequestMappingHandlerAdapter is done after the handler had a chance to fill the response and add HTTP headers
then RMHA can add "Cache-Control" directives, only if none have been added already by the handler
Note that in your case, I think your Controller is probably annotated with @SessionAttributes, which means that it's involved in a session conversation. So I'd suggest to move that handler method (adding "Cache-Control") to another Controller if it's not strictly related to that session conversation.
Thanks for the fix. I did a quick test with the 4.2.5-BUILD-SNAPSHOT and it seems to be fine now.
Just for clarification:
I do not use any SessionAttributes or other annotations within this controller. Basically I have some "pseudo-static" content that needs to be generated only once the first time some request reaches this controller. Once the result is built it will not change until the next version, therefore it can be cached the same way as other static content.
There are two use-cases where I use it:
I18N json for an angular service / "rest" endpoint.
Angular Service layer generation based on RequestMappings scanned at startup.
I will have to test a bit further, but i think this is fine now. Thanks again :)
Activity
spring-projects-issues commentedon Jan 22, 2016
Brian Clozel commented
Hi Martin Frey
Thanks for this report!
This is now fixed both in master and 4.2.x, meaning you can test this fix with
4.2.5.BUILD-SNAPSHOTor4.3.0.BUILD-SNAPSHOTin a few minutes.This is a two-part fix:
"Cache-Control"processing done by theRequestMappingHandlerAdapteris done after the handler had a chance to fill the response and add HTTP headersRMHAcan add"Cache-Control"directives, only if none have been added already by the handlerNote that in your case, I think your Controller is probably annotated with
@SessionAttributes, which means that it's involved in a session conversation. So I'd suggest to move that handler method (adding"Cache-Control") to another Controller if it's not strictly related to that session conversation.Don't hesitate to add some more comments here.
spring-projects-issues commentedon Feb 1, 2016
Martin Frey commented
Thanks for the fix. I did a quick test with the 4.2.5-BUILD-SNAPSHOT and it seems to be fine now.
Just for clarification:
I do not use any SessionAttributes or other annotations within this controller. Basically I have some "pseudo-static" content that needs to be generated only once the first time some request reaches this controller. Once the result is built it will not change until the next version, therefore it can be cached the same way as other static content.
There are two use-cases where I use it:
I will have to test a bit further, but i think this is fine now. Thanks again :)