我使用Spring Boot 1.0.2实现了一个REST服务器。我在防止Spring设置禁用HTTP缓存的HTTP头方面遇到了一些问题。
我的控制器如下:
@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);
}
}
所有HTTP响应都包含以下头部信息:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
我尝试了以下方法来移除或更改这些头部信息:
- 在控制器中调用
setCacheSeconds(-1)
。 - 在控制器中调用
httpResponse.setHeader(“Cache-Control”,“max-age = 123”)
。 - 定义
@Bean
,返回WebContentInterceptor
,并对其调用setCacheSeconds(-1)
。 - 在
application.properties
中将属性spring.resources.cache-period
设置为-1或正值。
以上方法均未生效。如何在Spring Boot中禁用或更改所有或个别请求的这些头部信息?