Reputation: 8709
Here is the method I have which responds back with JSON response. This code works on GlassFish but I'm getting an error when I run on tomcat 7.0.27
HTTP Status: 406
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
@RequestMapping(value = "/test", headers="Accept=application/json", method = RequestMethod.POST)
public @ResponseBody
Map<String, ? extends Object> getAuthPOST(@RequestBody String jsonBody) throws JSONException {
JSONObject j = new JSONObject(jsonBody);
System.out.println(j.keys());
Iterator i = j.keys();
Map<String, Object> result = new HashMap<String, Object>();
while (i.hasNext()) {
String key = (String) i.next();
result.put(String.valueOf(key), String.valueOf(j.getString(key)));
}
return result;
}
UPDATE:
Here are the headers when I get the request back
HTTP/1.1 406 Not Acceptable Content-Length: 1070 Server: Apache-Coyote/1.1 Content-Type: text/html;charset=utf-8 Date: Wed, 02 May 2012 15:47:24 GMT Connection: close
It changes the Content-Type to text/html.
Here are my request headers:
POST /SpringMVC/login/test HTTP/1.1 Accept: application/json Content-Type: application/json
Upvotes: 1
Views: 1411
Reputation: 26584
You haven't mentioned which version of spring-mvc you are using, but if you are on 3.1 you can add the "produces" parameter to your @RequestMapping. produces="application/json"
Upvotes: 0