users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: James Green <james.mk.green_at_gmail.com>
Date: Wed, 8 Aug 2012 07:33:40 +0100

On 7 August 2012 23:38, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:

> On Tue, Aug 7, 2012 at 2:54 PM, James Green <james.mk.green_at_gmail.com>
> wrote:
> > 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?
>
> This does seem odd: the default behavior, if annotation is not used
> (that, or Jackson's @JsonRootName), would be to use simple class name
> of the value class.
>

Mind taking a look?

Customer model in full: http://pastebin.com/RjdTvagY
JAX-RS Activator: http://pastebin.com/9v2CkbLz

Maven:
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.0.2</version>
        </dependency>
(Latest version offered)


>
> > 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?
>
> It depends on how you do it -- if it is when initializing
> ObjectMapper, or via ObjectWriter, it is safe.
> Actually, with jackson 2.0, there should not be any unsafe way.
>

I'll stick with jackson 2.0. I assume if I ship the library as a maven
dependency it should skip straight in to glassfish and jboss without
complaint?

>
> > 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.
>
> Ok, I hope it works out; and I will try to help as much as possible,
> may well help many others who do the same.
>
>
I'll try and write-up my experiences of this stuff for others to follow as
it's a right PITA particularly for those new to the platform I'm sure.

James