users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Tue, 7 Aug 2012 09:11:26 -0700

On Tue, Aug 7, 2012 at 12:03 AM, James Green <james.mk.green_at_gmail.com> wrote:
> Tatu,
>
> Thanks very much for your thoughts. I'm not adverse to your suggestion.
>
> Looking at the documentation and at the zip file sample, I'm still
> struggling to see how this can be made portable. Is there some magic I am
> missing/misunderstanding that developers use to customise their applications
> when deployed to certain containers or something?

I actually am not too familiar with Jersey documentation. Which
specific page/section are you thinking about?
My impression is that documentation may be bit behind latest practices
unfortunately.
For example, [http://jersey.java.net/nonav/documentation/latest/json.html]
is very long, yet does not actually contain the simplest (IMO)
solution. It does however start with POJO mapping.

> If I were to port the sample linked from the Jersey docs into application
> and deploy to JBoss surely it'll complain about the jersey dependencies?

Yes. So this would be one unfortunate thing about using Jersey's pojo
mapping enabling feature, since it is as much jersey-specific as
others (natural etc).

The way I have registered plain data-binding JSON object mapper for
Jersey and RESTeasy has been by using JAX-RS
`javax.ws.rs.core.Application`, which allows specifying objects that
provide (aka Providers) handlers of various kinds. For JSON support,
this means `MessageBodyReader` (read JSON to produce Java objects) and
`MessageBodyWriter` (write Java objects as JSON). Jackson project has
module [https://github.com/FasterXML/jackson-jaxrs-json-provider] that
implements both.

One link that might help is
[http://stackoverflow.com/questions/4330363/how-do-i-register-the-jackson-provider-with-the-wink-client]
since while it talks about Wink client, Application part is relevant
for general cases.

With that, what needs to be done is:

(a) construct `com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider`
(if using Jackson 2.0; 1.x version is under package
`org.codehaus.jackson.jaxrs`)
(b) return instance of that provider from Application.getSingletons() method

and with Jackson 2.0, in fact, even less is needed: you _should_ just
be able to add the provider jar in classpath, and it should be
auto-detected (1.x jar did not do this, since we were not sure if it
should override default mappings).

> Thanks for your continuing comments.

No problem!

-+ Tatu +-