Skip to content

ResponseEntity CacheControl ignored / extended by RequestMappingHandlerAdapter [SPR-13867] #18440

Closed

Description

@spring-projects-issues

Martin Frey opened SPR-13867 and commented

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.

Controller

@RequestMapping(value = "/somejson", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<TopLevel> getCurrent() {
    TopLevel t = new TopLevel();
    return ResponseEntity.ok().cacheControl(CacheControl.maxAge(6000, TimeUnit.SECONDS)).body(body);
}

Resulting in this header

Cache-Control=no-cache, no-store, max-age=0, must-revalidate, max-age=6000

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

@Override
protected ModelAndView handleInternal(HttpServletRequest request,
			HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

	checkRequest(request);

	if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
		applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
	}
	else {
		prepareResponse(response);
	}
	.
	.
	.
}

FROM WebContentGenerator
/**
 * 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
 */
protected final void prepareResponse(HttpServletResponse response) {
	if (this.cacheControl != null) {
		applyCacheControl(response, this.cacheControl);
	}
	else {
		applyCacheSeconds(response, this.cacheSeconds);
	}
}

Affects: 4.2.3

Issue Links:

Referenced from: commits 0ef90df, 50bcd67, fc34b0c, 8f1d06f

Activity

  1. spring-projects-issues commented on Jan 22, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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-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.

    Don't hesitate to add some more comments here.

  2. spring-projects-issues commented on Feb 1, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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:

    • 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 :)

  3. added
    type: bug
    in: web
    on Jan 11, 2019
  4. added this to the 4.2.5 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions