Sunday, 18 August 2013

How to use @RequestBody with a JSONP request?

How to use @RequestBody with a JSONP request?

I am trying to perform an ajax request using the jsonp data type due to
cross domain issues in a clustered environment.
I can make jsonp requests to methods mapped with no @RequestBody
parameters, but when I do try to implement a RequestMapping with a
@RequestBody parameter I get a 415 Unsupported Media Type error.
Usually when I get this problem it is due to some property not mapping
correctly between the json object posted and the Java object it is mapped
to in Spring. But the only discrepency I can find is that using jsonp it
adds a parm named callback and one with the name of an underscore "_"
So I added the tag @JsonIgnoreProperties(ignoreUnknown = true) to my Java
object and figured that should solve that, however it is still throwing
this error.
Is there anything else I need to do?
$.ajax({
url : 'http://blah/blah.html',
data : { abc : '123' }, (I also tried to JSON.stringify the object but
no difference)
dataType : 'jsonp',
success : function(response) {
alert('ok '+JSON.stringify(response));
},
fail : function(response) {
alert('error'+JSON.stringify(response));
}
});
The Spring controller is:
@RequestMapping({ "blah/blah" })
@ResponseBody
public ReturnObject getBlahBlah (@RequestBody MyObject obj) throws
Exception {
}
The parameter object is:
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject {
private String abc;
// getter and setter for abc auto generated by MyEclipse
}
I have a breakpoint on the Controller method which is never hit.

No comments:

Post a Comment