users@jersey.java.net

Re: application/json programming model

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 31 Aug 2007 17:56:46 +0200

Arun Gupta wrote:
>> The JAXB provider will automagically convert the returned States class
>> instance into JSON for you.
> This is reasonable. I tried it exactly this way without looking at the
> sample and got an empty response.
>
>> I'm sure Jakub's example will show this in more detail.
>
> This sample is not configured to run by default:
>

See the docs:

https://jersey.dev.java.net/source/browse/*checkout*/jersey/trunk/jersey/examples/StorageService/README.html

I need to document the command line options....

Paul.

> -- cut here --
> init:
> deps-jar:
> Processing resource classes
> compile:
> run:
> Exception in thread "main" java.lang.IllegalArgumentException
> at com.sun.ws.rest.samples.storageservice.Main.main(Main.java:85)
> Java Result: 1
> BUILD SUCCESSFUL (total time: 4 seconds)
> -- cut here --
>
> A default command-line argument is missing.
>
> -Arun
>>
>> Marc.
>>
>>>>> use. You might not be able to get the exact JSON format you show
>>>>> below but I'd bet you could get close just using some of the JAXB
>>>>> annotations.
>>>>>
>>>>> Marc.
>>>>>
>>>>> On Aug 30, 2007, at 2:33 PM, Arun Gupta wrote:
>>>>>
>>>>>> In order to return a JSON array like:
>>>>>>
>>>>>> [{"name":"California","value":"California"},{"name":"New
>>>>>> York","value":"New York"},{"name":"Alabama" ,"value":"Alabama"},
>>>>>> {"name":"Texas","value":"Texas"}]
>>>>>>
>>>>>> I have to write the following code:
>>>>>>
>>>>>> -- cut here --
>>>>>> @HttpMethod("GET")
>>>>>> @ProduceMime("application/json")
>>>>>> public JSONArray getMessage() throws JSONException {
>>>>>> String[] states = { "California", "New York", "Alabama",
>>>>>> "Texas"};
>>>>>> JSONArray array = new JSONArray();
>>>>>> for (String s : states) {
>>>>>> JSONObject item = new JSONObject();
>>>>>> item.put("name", s).put("value", s);
>>>>>> array.put(item);
>>>>>> }
>>>>>>
>>>>>> return array;
>>>>>> }
>>>>>> -- cut here --
>>>>>>
>>>>>> I think this is too involving and low-level. Can the code be
>>>>>> something like the following ?
>>>>>>
>>>>>> -- cut here --
>>>>>> @JSONObject
>>>>>> ItemBean {
>>>>>> ItemBean(String name, String value) { ... }
>>>>>>
>>>>>> @JSONObjectKey
>>>>>> public String getName() { ... }
>>>>>>
>>>>>> @JSONObjectValue("value")
>>>>>> public String getNameValue() { ... }
>>>>>> }
>>>>>>
>>>>>> @HttpMethod("GET")
>>>>>> @ProduceMime("application/json")
>>>>>> public List<ItemBean> getMessage() {
>>>>>> String[] states = { "California", "New York", "Alabama",
>>>>>> "Texas"};
>>>>>> List<ItemBean> list = new ArrayList<ItemBean>();
>>>>>> for (String s : states) {
>>>>>> ItemBean bean = new ItemBean(s, s);
>>>>>> list.add(bean);
>>>>>> }
>>>>>>
>>>>>> return list;
>>>>>> }
>>>>>> -- cut here --
>>>>>>
>>>>>> This will require defining new annotations JSONObject,
>>>>>> JSONObjectKey & JSONObjectValue. I think this is more natural to
>>>>>> a Java developer.
>>>>>>
>>>>>> -Arun
>>>>>> --
>>>>>> Web Technologies and Standards
>>>>>> Sun Microsystems, Inc.
>>>>>> Blog: http://blogs.sun.com/arungupta
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>
>>>>> ---
>>>>> Marc Hadley <marc.hadley at sun.com>
>>>>> CTO Office, Sun Microsystems.
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>> --
>>> Web Technologies and Standards
>>> Sun Microsystems, Inc.
>>> Blog: http://blogs.sun.com/arungupta
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>> ---
>> Marc Hadley <marc.hadley at sun.com>
>> CTO Office, Sun Microsystems.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109