title: How to support JSONP with Spring MVC and multiple response types
body: <p>I have a method in a controller that will return HTML or JSON depending on which was asked for. Here's a stripped-down example of such a method, modeled after information on how to do this that I found in <a href="https://stackoverflow.com/questions/4403643/supporting-multiple-content-types-in-a-spring-mvc-controller/">this question</a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RequestMapping(value="callback")
  public ModelAndView callback(@RequestParam("c") String c) {
      Map response = new HashMap&lt;String, String&gt;();
      response.put("foo", "bar");
      return new ModelAndView("fake", "data", new JSONPObject(c, response));
  }
  </code></pre>
  
  <p>I put the JSONPObject into the model because I have to in order to be able to reach it from the view that renders if HTML was requested. But this poses a problem when I'm rendering JSON with a callback:</p>
  
  <pre><code>curl 'http://localhost:8080/notes/callback.json?c=call'
  {"data"call(:{"foo":"bar"})}
  </code></pre>
  
  <p>As you can see, because I put my data in the "data" slot in the model, when the model is rendered as JSON there's that extra wrapping. What I'm looking for is the rendered JSON (technically JSONP) to look like this:</p>
  
  <pre><code>call({"data":{"foo":"bar"}})
  </code></pre>
  
  <p>Can anyone see a way to get where I'm trying to go without breaking the ability to return a view that accesses the JSONPObject during rendering?</p>