users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Wed, 8 Aug 2012 08:01:33 -0700

On Tue, Aug 7, 2012 at 11:33 PM, James Green <james.mk.green_at_gmail.com> wrote:
>
>
> 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

I'll have a look, see if anything jumps out.

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

2,0.5 is actually available now:
http://repo1.maven.org/maven2/com/fasterxml/jackson/jaxrs/jackson-jaxrs-json-provider/
although it does not seem to have any fixes relevant to your problem.

One thing you may also want to add (if not already included) is the
JAXB annotation (support) module:

https://github.com/FasterXML/jackson-module-jaxb-annotations

>>
>> > 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?

Yes.

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

Agreed. It's a pity, too, given how simple most other aspects are.

I think this is also one reason why use of DropWizard
(https://github.com/codahale/dropwizard) can simplify life -- it
builds on Jersey, but everything comes configured out of the box, so
there's even less hassle.

-+ Tatu +-