I am not able to pass my own object through @FormParam with Jersey. It is always null. I have method like this:
@POST
@Path("search")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public SearchResult search(
@FormParam("record") MyObject record,
@FormParam("offset") Integer offset,
...) {
...
}
When I try to pass the MyObject, it’s always null. The parameter offset is passed correctly.
Client side in JavaScript for illustration:
$.ajax({
'url': url,
'type': 'POST',
'dataType': 'json', 'contentType': 'application/json',
'data': {'record': record, 'offset': 123}
})
I thought that it might be a problem with jQuery, so I tried to change the parameter to some nonsence 'data': {'record': 'string instead of json', 'offset': 123} and I got 400 (Bad Request). I also checked the body of the request and it seemed alright.
But when I change the method to look like this:
public SearchResult search(MyObject record) { ... }
And the client script accordingly:
$.ajax({
'url': url,
'type': 'POST',
'dataType': 'json', 'contentType': 'application/json',
'data': JSON.stringify(record)
})
The MyObject is obtained correctly.
I tried to use GET method with the same results.
Is it possible that it’s a bug in Jersey or am I missing something?
@FormParam only works for application/x-www-form-urlencoded media type. So, the second approach (the one that works for you) is the right way to receive JSON message entities.