title: Intercepting the @responsebody in spring mvc
body: <p>I have a Spring MVC web application with conroller like below :</p>
  
  <pre><code>@Controller
  public class ActionRestController {
  @RequestMapping(value = "/list", method = GET)
  @ResponseBody
  public List&lt;Action&gt; list(Action action, SearhCriteria searchCriteria) {
       List&lt;Action&gt; ret = new ArrayList&lt;Action&gt;();
       // Call a service method to get the records
       // Copy the records into the list
       // return the list of objects
       return ret;
  }
  </code></pre>
  
  <p>The Controller is invoked when the user does a search. There are several such controllers in the app, one for each searchable entity.
  For reasons that I cannot explain very well, here, I cannot modify these controllers in anyway.</p>
  
  <p>But now, I have requirement in the UI to display the search criteria and the no. of records and paging details, as well. This information is not returned by the controller. The JSON returned by the Controller contains just the list of records.</p>
  
  <p>I have put up a different controller which will handle the request, gets and puts the extra info in the model and forwards the request to the existing controller like below :</p>
  
  <pre><code>@Controller
  public class ActionExtendedController {
  @RequestMapping(value = "/searchlist", method = GET)
  @ResponseBody
  public List&lt;Action&gt; list(Action action, SearhCriteria searchCriteria, Model model) {
       model.addAttribute("searchParameters", searchCriteria);
       return "forward:/list";
  }
  </code></pre>
  
  <p>Upto this point, all is well.
  What I want to do is intercept the request at a point where the List is returned from the controller, before it is converted to JSON, and return a map containing the list and the search parameters.</p>
  
  <p>Now since the 'immutable' controller users ResponseBody the control goes to the JacksonMessageConverter amd the response goes out from there. I have already tried the following paths and they do not work.</p>
  
  <ul>
  <li><p>Interceptor - By the time I get here, the response is already written out, so there is no way to change it.</p></li>
  <li><p>Custom ObjectMapper for the JasksonMessageConverter - Will not work, since I do not have access to the model object inside the mapper, I only have access to the list returned by the controller.</p></li>
  <li><p>Aspect @After pointcut for the controller - I think this technique will work, but I cannot get it to work. The advise does not fire and I am sure I am missing something in the configuration.</p></li>
  </ul>
  
  <p>Is there a way to get Spring AOP to fire on a annotated controller, handler method or
  can anyone suggest another method of intercepting the handler return value (along with the model) ?</p>