dev@glassfish.java.net

Re: How to POST a list of strings ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 14 Nov 2008 10:28:49 +0100

On Nov 14, 2008, at 10:05 AM, Farjola Zaloshnja wrote:

> will this work ?
>
> ClientResponse resp = c.handle(cr);
>
> JSONObject jobj =
>
> new JSONObject(resp.toString());
>
> JSONArray jas =
>
> new JSONArray().put(jobj);
>

No. I am guessing you want to receive back a JSONArray from the POST.

  Client c = Client.create();
  WebResource r = c.resource("http://host/path");

  JSONArray ja = new JSONArray(Arrays.asList("foo", "bar"));
  ja = r.type("application/json").post(JSONArray.class, ja);

With a resource class with the following resource method:

   @Path("path")
   @POST
   @Consumes("application/json");
   public JSONArray post(JSONArray ja) {
     return ja;
   }

If you want to get the full ClientResponse to get access to the status
code and response headers do:

  JSONArray ja = new JSONArray(Arrays.asList("foo", "bar"));
  ClientResponse cr = r.type("application/
json").post(ClientResponse.class, ja);
  ja = cr.getEntity(JSONArray.class);


Note that it is not necessary to explicitly build a ClientRequest, it
is built implicitly when you use WebResource (and the uniform
interface).


Online JavaDoc for the client is here:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0/api/jersey/com/sun/jersey/api/client/package-summary.html

and you can download it from here:

   http://download.java.net/maven/2/com/sun/jersey/jersey-client/1.0/jersey-client-1.0-javadoc.jar

If you are using NetBeans and the Maven plugin then in the Libraries
of your project NetBeans will recognize there is JavaDoc present and
it is really easy to browse by selecting View JavaDoc from the right
mouse click menu.

Paul.

> On Thu, Nov 13, 2008 at 10:30 AM, Farjola Zaloshnja <farjola.zaloshnja_at_gmail.com
> > wrote:
> Hi,
> I haven't used much the POST function in the jsr311 framework but if
> I were to post a list of strings will this be correct?
>
> On the client side:
>
> Client c = Client.create();
>
> URI url =
>
> new URI("getmylist");
> ClientRequest cr = ClientRequest.create().entity(List<String> mylist,
>
> "application/json").build(url, "POST");
> And the server side receiving will look like:
>
>
> @POST
>
> @Path("getmylist")
>
> public void getmylist(List<String> programids){ }
>
> Any comments ?
>
> Thank you,
>
> Farjola
>
>
>
>
>