My spring3.1 mvc server can response right json result by add @ResponseBody
, should no problem on spring setting file. The servlet.xml included
<mvc:annotation-driven/>
I put jackson jars in classpath.
And my html file also set contentType to 'application/json" in .ajaxSetup() method. And I can see post header is right in Firebug.
I also tried include json2.js to call JSON.stringify method.
The json format sample:
{"link":"http:xxxxx","ticker":"xxxxx"}
The responseBody class is Document:
public class Document implements Serializable{
public String getLink(){...}
public String setLink(String link){...}
public String getTicker(){...}
public String setTicker(String ticker){...}
}
The controller code:
public class LoadFile {
@RequestMapping(value="/loadFile",method=RequestMethod.POST)
public @ResponseBody Document loadFile(@RequestBody Document document){
...
}
}
The html code:
$(function() {
$.ajaxSetup({
contentType:"application/json;charset=utf-8"
});
$.post( url, { link: link, ticker:ticker },
function( data ) {
....
},"json"
);
}
Document class can return as a json object when added @ResponseBody
, but when as a @RequestBody
, the server returns 400 error. Not sure what happens.