users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: James Green <james.mk.green_at_gmail.com>
Date: Tue, 7 Aug 2012 22:54:21 +0100

Tatu,

See inline responses.

On 7 August 2012 17:11, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:
>
>
> 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.
>
[ ... ]

> 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).
>


OK, I've hooked up a real simple project with a Customer bean having
@XmlRootElement. It works out the of box with an Application activator
returning a JacksonJaxbJsonProvider.

I've switched on SerializationFeature.WRAP_ROOT_VALUE to mirror our XML api
documentation a little closer. Two things are otherwise immediately
apparent:

1. The JSON has an empty root name:

{
    "": {
        "id": 2
    }
}

Unless I expressly set the name of the @XmlRootElement. Can't find
reference to this on Google though. Any ideas?

2. I have switched off SerializationFeature.WRITE_DATES_AS_TIMESTAMPS again
to mirror our XML API docs closer. My reading of the JavaDocs suggests that
although not thread-safe by itself, the formatter is here only used by
thread-safe framework methods. Is this correct?

I've run out of time to investigate tonight. I'll continue with my tiny app
to try and get a representation does not differ from XML unnecessarily and
post an update.

Thank you once more for your time.

James