我正在使用 Spring 3.0 并且有如下内容
@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/someUrl")
public String handleSomeUrl(@RequestParam(required = true) Long someId){
// ...
return someString;
}
我有一个带有 postHandle 的拦截器,它被调用并在响应中设置一些无缓存 header 。但是,当响应返回到浏览器时,所有这些缓存 header 都消失了。有趣的是,如果我删除 @ResponseBody 注释并改用响应编写器,我会看到拦截器设置的 header 。为什么 @ResponseBody 覆盖了我的 header ?
注意:
如果我执行以下操作,我还会在响应中看到正确的 header 。这与我的拦截器的 postHandle 做的完全一样。
@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/someUrl")
public String handleSomeUrl(@RequestParam(required = true) Long someId, HttpServletRequest request, HttpServletResponse response){
response.setHeader("Cache-Control","no-store, no-cache, must-revalidate");
return someString;
}
最佳答案
可能与此有关 open issue. .它有以下评论:
I don't think it's wise to mix and math the use of HttpServletResponse and @ResponseBody.
关于java - @ResponseBody 注解在响应中重置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5161941/