Reputation: 10629
I am trying to make something very similar to this post using Spring MVC 3.1:
JQuery, Spring MVC @RequestBody and JSON - making it work together
and trying to get the response as a custom class such as "FooBar" in the example.
my ajax call looks like this:
// var rule = "giveThisATry";
var rule = {
id : 1,
name : {
name1 : "1",
name2 : "2",
name3 : "3",
name4 : "4"
}
};
$.ajax({
url : "http://localhost:8080/spring2/save",
type : "POST",
dataType : "json",
//contentType : "application/json; charset=utf-8",
data : rule,
success : function(data) {
alert("success saveRule: " + data);
},
error : function(request, status, error) {
alert("error saveRule: " + request.responseText);
}
});
and my rest method looks like this:
@RequestMapping(method = RequestMethod.POST, value = "/save")
public @ResponseBody MyClass save(@RequestBody MyClass instance, final HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Origin", "*");
return MyDB.Save(instance);
}
Now no matter what I do, Spring would not map the ajax call to this method. Also if i uncomment the line in my ajax call :
contentType : "application/json; charset=utf-8",
Chrome would change the "request-method" to "OPTIONS" :
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:Origin, Content-Type, Accept
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:8080
Origin:http://127.0.0.1:8020
Pragma:no-cache
Referer:http://127.0.0.1:8020/JQueryPOC/src/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
Response Headersview source
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
Content-Length:0
Date:Fri, 15 Jun 2012 15:07:19 GMT
Server:Apache-Coyote/1.1
Now how can i test/debug/have control over the MappingJacksonHttpMessageConverter so that i can map the json to a pojo using requestbody or something else ?
Upvotes: 0
Views: 1367
Reputation: 49925
One thing I see that you have to convert the rule
to a Json String - this plugin may help you:http://code.google.com/p/jquery-json/
data : $.toJSON(rule)
Upvotes: 1