@ResponseBody Reply Header Reseller Annotations

I am using Spring 3.0 and have something like the following

@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/someUrl")
public String handleSomeUrl(@RequestParam(required = true) Long someId){
    // ...
    return someString;
}

I have an interceptor with postHandle that gets called and sets multiple cache headers in the response. However, when the response returns to the browser, all of these cache headers have disappeared. The funny thing is that if I delete the @ResponseBody annotation and use the answering machine instead, I see the headers set by the interceptor. Why is @ResponseBody overriding my headers?

Note:

If I do the following, I will also see the correct headers in my answer. This does the same thing as my postHandle interceptor.

@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;
}
+3
source share
2 answers

.. :

, HttpServletResponse @ResponseBody.

+1

preHandle ( Spring , postHandle, postHandle , @ResponseBody):

public class ControllerHandleInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       if (handler instanceof HandlerMethod) {
          response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 
          response.setHeader("Pragma", "no-cache"); 
          response.setHeader("Expires", "0"); 
       }

       return true;
    }

    // other code...
}
0

Source: https://habr.com/ru/post/1795632/


All Articles