title: Spring return JSON / XML / JSON-P based on suffix
body: <p>I have set up my controller so that it will return the data in the format as requested through HTTP Accept-Type header set by the client:</p>
  
  <pre><code>&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;ref bean="jacksonJSONMessageConverter" /&gt;
              &lt;ref bean="jaxbXMLConverter" /&gt;
              &lt;ref bean="jsonpMessageConverter" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Sample controller method:</p>
  
  <pre><code>@RequestMapping(value = "/test", method = RequestMethod.POST)
  @ResponseBody
  public TestObject executeTest()
  {
      TestObject t = ...
      // not important, generating t
      return t;
  }
  </code></pre>
  
  <p>so for example they will do: http:// someurl/test</p>
  
  <p>It works perfectly just fine <strong>if</strong> the client can actually set Accept-Type. Now that's where the issue starts when client is unable to set Accept-Type header, I would rely on the url to be suffixed, for example:</p>
  
  <ul>
  <li>http:// someurl/test.xml</li>
  <li>http:// someurl/test.json</li>
  <li>http:// someurl/test.jsonp?callback=fn</li>
  </ul>
  
  <p>My challenge is how to configure Spring properly to do this?</p>
  
  <p>Some suggestions:</p>
  
  <ul>
  <li>use some static method to return json: <a href="https://stackoverflow.com/questions/4917329/return-json-or-view-from-spring-mvc-controller">Return JSON or View from Spring MVC Controller</a></li>
  <li>use ResponseEntity <a href="https://stackoverflow.com/questions/4471584/in-spring-mvc-how-can-i-set-the-mime-type-header-when-using-responsebody">In Spring MVC, how can I set the mime type header when using @ResponseBody</a> </li>
  <li>use default views: <a href="http://blog.safaribooksonline.com/2012/03/28/spring-mvc-tip-returning-json-from-a-spring-controller/" rel="nofollow noreferrer">http://blog.safaribooksonline.com/2012/03/28/spring-mvc-tip-returning-json-from-a-spring-controller/</a></li>
  </ul>
  
  <p>and many others, but none of the solution seem to be able to satisfy what I need in a nice, clean why. Ideally I would like to be able to do something clean like</p>
  
  <pre><code>@RequestMapping(value = "/test.xml", method = RequestMethod.POST)
  @ResponseBody
  public TestObject executeTestReturnXML()
  {
      TestObject t = executeTest();
  
      return t; // somehow magically force Spring converter to convert it to XML
  }
  
  @RequestMapping(value = "/test.json", method = RequestMethod.POST)
  @ResponseBody
  public TestObject executeTestReturnJson()
  {
      TestObject t = executeTest();
  
      return t; // somehow magically force Spring converter to convert it to JSON
  }
  
  @RequestMapping(value = "/test.jsonp", method = RequestMethod.POST)
  @ResponseBody
  public TestObject executeTestReturnJsonP(@RequestParam(value = "callback", required = true) String callback)
  {
      TestObject t = executeTest();
  
      return t; // somehow magically force Spring converter to convert it to JSON-P with callback wrapper
  }
  </code></pre>
  
  <p>Suggestions and/or directions would be greatly appreciated!</p>