title: How can I wrap a JSON response in Spring
body: <p>Suppose I have two sets of controllers in Spring:</p>
  
  <ul>
  <li><code>/jsonapi1/*</code></li>
  <li><code>/jsonapi2/*</code></li>
  </ul>
  
  <p>both of which return objects that are to be interpretted as JSON text.</p>
  
  <p>I'd like some kind of filter to wrap the responses from one set of these controllers so that:</p>
  
  <ol>
  <li><p>the original response is contained within another object.</p>
  
  <p>For example, if /jsonapi1/count returns:</p>
  
  <pre><code>{"num_humans":123, "num_androids":456}
  </code></pre>
  
  <p>then the response should be wrapped and returned as follows:</p>
  
  <pre><code>{ "status":0,
    "content":{"num_humans":123, "num_androids":456}
  }
  </code></pre></li>
  <li><p>if an exception happens in the controller, then filter should catch the exception and report it as follows</p>
  
  <pre><code>{ "status":5,
    "content":"Something terrible happened"
  }
  </code></pre></li>
  <li><p>The responses from the other controllers are returned unchanged.</p></li>
  </ol>
  
  <p>We're currently customizing a <code>MappingJackson2HttpMessageConverter</code> passed to <code>WebMvcConfigurerAdapter.configureMessageConverters</code> in order to perform the above tasks. Works great except that it doesn't seem possible for this approach to be selective about the URLs (or controller classes) it applies to.</p>
  
  <p>Is it possible to apply these kinds of wrappers to individual controller classes or URLs?</p>
  
  <hr>
  
  <p>Update: Servlet filters look like a solution. Is it possible chose which filter gets applied to which controller methods, or which URLs?</p>