title: Post processing of a Json response in spring MVC
body: <p>I have several controllers that return the same generic Response object with @ResponseBody annotation, like this:</p>
  
  <pre><code>@RequestMapping(value = "/status", method = RequestMethod.GET)
      @Transactional(readOnly = true)
      public @ResponseBody Response&lt;StatusVM&gt; status()
  </code></pre>
  
  <p>I need to perform an operation on every controller, after the Response is returned. This operation will enrich the Response object with new data.</p>
  
  <p>I don't want to duplicate code, so I need a single point of intervention. I thought I could do this with Interceptors, however, according to the docs <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor</a> this does not work well with @ResponseBody:</p>
  
  <p><em>Note that the postHandle method of HandlerInterceptor is not always ideally suited for use with @ResponseBody and ResponseEntity methods. In such cases an HttpMessageConverter writes to and commits the response before postHandle is called which makes it impossible to change the response, for example to add a header. Instead an application can implement ResponseBodyAdvice and either declare it as an @ControllerAdvice bean or configure it directly on RequestMappingHandlerAdapter.</em></p>
  
  <p>I haven't been able to find an example of this tecnique, could anybody help me?</p>
  
  <p>As an alternative I could work with aspects, but then I'd need to annotate every controller, which is something I'd like to avoid.</p>