Skip to content

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

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions