dev@glassfish.java.net

Fwd: How to POST a list of strings ?

From: Farjola Zaloshnja <farjola.zaloshnja_at_gmail.com>
Date: Fri, 14 Nov 2008 09:58:07 +0100

---------- Forwarded message ----------
From: Farjola Zaloshnja <farjola.zaloshnja_at_gmail.com>
Date: Fri, Nov 14, 2008 at 9:55 AM
Subject: Re: How to POST a list of strings ?
To: Paul Sandoz <Paul.Sandoz_at_sun.com>


Hi,

As I mentioned before I am utilizing Jettison, and using the JSAONArray on
posting a list of strings, one question though I have while coding my
client-server - if I send a response on the POST ( a JSONArray) how would I
be able to get that back to a JSONArray :

Snip of code:

Client c = Client.create();

URI url =
*new* URI("mypath");

Long[] ia = (Long[]) programids.toArray();

JSONArray jarray =
*new* JSONArray(Arrays.asList(ia));

ClientRequest cr = ClientRequest.create().entity(jarray,
"application/json").build(url, "POST"
);

ClientResponse resp = c.handle(cr); ---> how to create a JSONArray :-/ could
you please provide with an example ?
Thank you in advance,
Farjola



 On Thu, Nov 13, 2008 at 11:12 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:

> Hi Farjola,
>
>
> On Nov 13, 2008, at 10:30 AM, Farjola Zaloshnja 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?
>>
>>
> Do you want to encode a list of strings as a JSON array? for example:
>
> ["foo", "bar", "baz"]
>
> If so you can utilize the Jettison [1] class
> org.codehaus.jettison.json.JSONArray class [2].
>
> From the client:
>
> Client c = Client.create();
> WebResource r = c.resource("http://host/path");
>
> JSONArray ja = new JSONArray(Arrays.asList("foo", "bar"));
> r.type("application/json").post(ja);
>
> With a resource class with the following resource method:
>
> @Path("path")
> @POST
> @Consumes("application/json");
> public void post(JSONArray ja) {
> ...
> }
>
> The reason List<String> does not work as you expect is that Jersey does not
> know how to serialize List<String> in the JSON format. If you want to
> support that you can add your own functionality utilizing MessageBodyWriter.
> See the following sample for more details:
>
>
> http://download.java.net/maven/2/com/sun/jersey/samples/entity-provider/1.0/entity-provider-1.0-project.zip
>
> Paul.
>
> [1] http://jettison.codehaus.org/
> [2] http://www.json.org/javadoc/org/json/JSONArray.html
> Note that Jettison copies the classes from JSON.org do the JavaDoc
> should still apply.
>
>
>
>> 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
>>
>>
>>
>>
>>
>