title: Spring MVC: How to modify json response sent from controller
body: <p>I've built a json REST service with controllers like this one:</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/scripts")
  public class ScriptController {
  
      @Autowired
      private ScriptService scriptService;
  
      @RequestMapping(method = RequestMethod.GET)
      @ResponseBody
      public List&lt;Script&gt; get() {
          return scriptService.getScripts();
      }
  }
  </code></pre>
  
  <p>It works fine, but now I need to modify all responses and add "status" and "message" fields to all of them. I've read about some solutions:</p>
  
  <ol>
  <li>return from all controller methods object of some specific class, for example,
  RestResponse, which will contain "status" and "message" fields (but it's
  not general solution, cause I will have to modify all my controllers
  and write new controllers in new style)</li>
  <li>intercept all controller methods with aspects (but in this case I can't change return type)</li>
  </ol>
  
  <p>Can you suggest some other, general and correct solution, if I want to wrap values returned from controller methods into objects of class:</p>
  
  <pre><code>public class RestResponse {
  
      private int status;
      private String message;
      private Object data;
  
      public RestResponse(int status, String message, Object data) {
          this.status = status;
          this.message = message;
          this.data = data;
      }
  
      //getters and setters
  }
  </code></pre>