How to enable HTTP response caching in Spring Boot
- by Samuli Kärkkäinen
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 returns WebContentInterceptor for which I've called setCacheSeconds(-1).
Set property spring.resources.cache-period to -1 or a positive value in application.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?