Kasas Kasas - 1 month ago 18x
Java Question

Jersey POST Method is receiving null values as parameters

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:

@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
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


{"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:

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:

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:


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:

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.