How to enable HTTP response caching in Spring Boot
Posted
by
Samuli Kärkkäinen
on Stack Overflow
See other posts from Stack Overflow
or by Samuli Kärkkäinen
Published on 2014-06-11T13:19:20Z
Indexed on
2014/06/12
9:25 UTC
Read the original article
Hit count: 710
I have implemented a REST server using Spring Boot 1.0.2. I'm having trouble preventing Spring from setting HTTP headers that disable HTTP caching.
My controller is as following:
@Controller
public class MyRestController {
@RequestMapping(value = "/someUrl", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> myMethod(
HttpServletResponse httpResponse) throws SQLException {
return new ResponseEntity<String>("{}", HttpStatus.OK);
}
}
All HTTP responses contain the following headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
I've tried the following to remove or change those headers:
- Call
setCacheSeconds(-1)
in the controller. - Call
httpResponse.setHeader("Cache-Control", "max-age=123")
in the controller. - Define
@Bean
that returnsWebContentInterceptor
for which I've calledsetCacheSeconds(-1)
. - Set property
spring.resources.cache-period
to -1 or a positive value inapplication.properties
.
None of the above have had any effect. How do I disable or change these headers for all or individual requests in Spring Boot?
© Stack Overflow or respective owner