title: How to populate ModelAndView in HandlerInterceptor for REST service in Spring
body: <p>I'm building a RESTfull service, using Spring framework.</p>
  
  <p>I need to add custom Cookie to response, after processing request in Controller, based on the response type</p>
  
  <pre><code>@Controller
  public class SomeController {
  
      ...
  
      @RequestMapping(method = RequestMethod.GET, value = MY_PROFILE, produces = PRODUCES)
      public @ResponseBody Some myProfile(@RequestBody() A a) {
          ...
      }
  
      ...
  }
  </code></pre>
  
  <p>But by the time it reaches my Interceptor</p>
  
  <pre><code>public class SomeIntercepter implements HandlerInterceptor {
  
      ...
  
      @Override
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      }
  
      ...
  
  }
  </code></pre>
  
  <p><strong>ModelAndView is NULL</strong>, and I can only work with request &amp; response.</p>
  
  <p>So I have 2 questions:</p>
  
  <ol>
  <li>Is there a way, to make Spring populate <strong>ModelAndView</strong>, with data, without <strong>working</strong> with <strong>HttpServletResponse</strong>, <strong>HttpEntity</strong> or <strong>ModelAndView</strong> explicitly, <strong>leaving Controller intact</strong>? <em>(I'm not going to use ModelAndView to add a Cookie, I just need to know response Object in Intercepter, and add Cookie to HttpServletResponse.)</em></li>
  <li>I was thinking of solving this, using <strong>ThreadLocal</strong>, but in my understanding, this would brake in case of asynchronous processing. Is there a proven <strong>ThreadLocal</strong> solution for this, that won't break in case I decide to switch to asynchronous processing?</li>
  </ol>
  
  <p>Example Use Case:</p>
  
  <p>Let's say that I'm doing A/B testing, and my UI, renders differently, based on the Cookie in Response.
  I would like to have a place where, based on the return object, I could specify this Cookie.</p>