I am developing RESTful services with Jersey and it works great with GET methods. However I can't make it work with POST methods and JSON or text parameters. These is what I did:
@Path("/method/")
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON})
public ResponseObject method(@Context Request request, @PathParam("ob1") Object obj1, @PathParam("obj2") String obj2) {
...
}
I am only getting null values for all params. I have tried to use just a string as parameter and it doesn't work either ... I am trying to access these methods from IOS and maybe that's one of the problems. However I have been sniffing my LAN and I can see the correct parameters in the packet body ... is this correct??
I have sent from XCode different body contents as:
obj1={"id1": "value1", "id2" : "value2"}&obj2=xxxx
and:
{"id1": "value1", "id2" : "value2"},xxxx
while I have been playing with @QueryParam and @PathParam without results...always null...
Thanks for your help!
A path parameter is a portion of the request URL matching a particular pattern. As such, there are character limitations on what can be specified as a path param - in particular any special characters need to be URL encoded. This applies the same across any kind of request (GET, POST, PUT, DELETE).
As a general rule, you should restrict your path parameters to simple values like identifiers, or resource endpoints - more complex data should be passed to the REST service via request parameters or the request body itself. Here's a mixed approach that passes an entity identifier as a path parameter, and the entity data in the request body:
@Path("/contacts/{id}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateContact(@PathParam final String contactId, Contact contact) {
}
In the above example, the contactId is obtained as a path parameter and the contact is serialized automatically from the request body.
What I described above are general rules. Now as to the specifics of your case, one thing I notice in your code is that you don't actually define any path parameters. Remember that they have to be defined as part of your @Path
annotation, before being consumed in your REST method:
@Path("/method/{obj1}/{obj2}")
public ResponseObject method(@Context Request request, @PathParam("obj1") Object obj1, @PathParam("obj2") String obj2) {
}
With the above changes your parameters should no longer show up as being null, assuming you have properly encoded the URL on the client side.
* EDIT *
Based on your comment, I see you need to become more familiar with the JAX-RS specification and the various parameter types. I recommend reading through the RESTEasy JAX-RS Documentation. It has some vendor specific implementation details but all in all is an excellent guide to JAX-RS.
Purpose: Used to inject a portion of the request URL into a variable. Note that URL parameters are not considered part of the URL.
Example: Given the URL http://services.example.com/contacts/20578, I can define:
@Path("/contacts/{id}")
From which I can inject a @PathParam("id")
.
public Response getContact(@PathParam("id") final String identifier);
This works for any kind of HTTP request (GET, POST, PUT, DELETE).
Purpose: Used to inject a portion of the query string or form encoded data into a variable. The query string is that portion of your URL after the ?
. Form encoded data are the URL encoded name/value pair data passed in the body of an HTTP request, when the request type is application/x-www-form-urlencoded. Typically, query parameters are passed as part of the URL string for GET requests, and in the request body for POST requests.
Example: Given the URL http://services.example.com/contacts?group=Business, I can inject a @QueryParam("group")
public Response getContactsInGroup(@QueryParam("group") final String groupName);
It's atypical to use query parameters with a POST request, but it is possible if the request type is application/x-www-form-urlencoded:
@POST
@Path("/contacts")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response createContact(@QueryParam("contact") final Contact contactData, @QueryParam("metadata") final String metaData);
These are just high level examples, please read through the documentation I linked to get a better example of how each parameter type works, and when to use which one.
I just started developing webservice in java and had a same problem with POST data. I got very simple solution to read the POST data using @FormParam, Actually i was using the @QueryParam to read POST data, I think its only for reading the QueryString data using GET method
A very nice documentation given here. Having read this article, most of my confusions were cleared.
http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html_single/index.html
Tip: Just make sure you are using "application/x-www-form-urlencoded" mime type while using @FormParam
I'd post this as a comment to the accepted answer, but I'm just shy of being able to do that.
In addition to the excellent advice above, I would add that, at least in version 2.0.x, Jersey does not pull @FormParam
from the query string. Instead, it expects it to be included as a name-value pair in the request body.
For example, instead of POST http://localhost/app?name=Joe
, you would send
POST http://localhost/app
with the body:
name=Joe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With