users@jersey.java.net

Re: [Jersey] JSON providers

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Wed, 16 Dec 2009 15:08:46 +0100

Hi Tom,

On Wed, Dec 16, 2009 at 09:23:01AM +0000, Tom wrote:
> Hi,
>
> I'm just starting out with jersey in order to produce JSON but after
> replicating a simple example I've become lost. JAXB annotations are
> new to me also so I'm currently trying to work out how get the exact
> output I need.
>
> The example I've been following is from chapter 1 of the jersey user
> guide (with grizzly) but producing application/json.
>
> I've seen that I have various options for generating JSON (Jersey,
> Jettison, Jackson, etc.) and there is mention of a JAXBContextResolver
> but I don't understand how these fit together, or even if they are the
> same thing.

JAXBContextResolver applies only when JAXB based JSON (de)serialization
is used. Then on the server side, the resolver is found automatically,
when the application packages are scanned by Jersey runtime.
On the client side, you would need to register the resolver manualy:

  ClientConfig config = new DefaultClientConfig();
  config.getClasses().add(YourJAXBContextResolver.class);
  Client client = Client.create(config);

>
> Can someone clarify how to use the JAXBContextResolver example
> provided in chapter 3? I don't see how to wire this up since there is

The most of the chapter 3, of the Jersey user guide, is about the above
mentioned JAXB based approach. As the user guide does not provide
a self contained complete example, please look at [1].

> seems to be a certain amount of automated magic in the basic example.
> JAXBContextResolver also looks like boiler plate code when it could be
> configuration/annotation - is this the only way?

The other two options are:

 i) low-level JSONObject/JSONArray based approach, where you have
    the best control of what is being generated (see [3])
ii) Jacskon JAX-RS provider (see [2] for a very simple example),
    which is capable of processing generic Java classes (no JAXB annotations needed)

HTH,

~Jakub


[1]http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.1.5-ea-SNAPSHOT/json-from-jaxb-1.1.5-ea-SNAPSHOT-project.zip
[2]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/jacksonjsonprovider/1.1.5-ea-SNAPSHOT/jacksonjsonprovider-1.1.5-ea-SNAPSHOT-project.zip
[3]http://download.java.net/maven/2/com/sun/jersey/samples/bookmark/1.1.5-ea-SNAPSHOT/bookmark-1.1.5-ea-SNAPSHOT-project.zip

>
> Thanks,
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
http://blogs.sun.com/japod