users@jersey.java.net

JSON as method arguments

From: <jmrodri_at_gmail.com>
Date: Tue, 15 Dec 2009 16:29:44 -0500 (EST)

I'm having a bit of a problem trying to POST JSON to a method with 2 or more arguments.

For example, I have a create(ConsumerInfo ci, String label) method in a ConsumerResource
class.

  @POST
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED})
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public Consumer create(ConsumerInfo ci, String label) ...

If I remove the label param, I am able to easily POST a JSON object and have it converted to a
ConsumerInfo quite easily. But once I add a second parameter, it always gets the value of null.

My client is a python script, though I've tried using the jersey java client as well. With the jersey
client, I'm not sure how to pass in multiple objects to the post() method

  WebResource postresource = c.resource("http://localhost:8080/candlepin/test/");
  postresource.accept("application/json").type("application/json").post(ci);

ci is my ConsumerInfo object, but looking at the javadoc for post() it always takes in either one
object, or one object and a return class type. How would I pass in the String?

From python, it's a similar problem but I'm not dealing with objects, more straight json.
So once I know what the json needs to look like, I can figure that part out.

Would I be able to pass in a List of parameters? For example,

  List<Object> params = new ArraysList<Object>();
  params.add(ci); // add my ConsumerInfo
  params.add("new-consumer");
  postresource.accept("application/json").type("application/json").post(params);

Anyone done something like this? The only other way I know how to do this is to get
rid of the POST of JSON and stick with APPLICATION_FORM_URLENCODED, but that
then leads me to how will this work with PUT then.

Thoughts? Advice?

Sincerely,
jesus