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 16:29:34 +0100

See below.

On 8 August 2012 16:10, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:

> On Wed, Aug 8, 2012 at 8:01 AM, Tatu Saloranta <tsaloranta_at_gmail.com>
> wrote:
> > 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.
>
> Ah. This is due to interaction between JAXB annotation introspector
> (Jackson component), and Jackson's root-wrapper. Empty String is not
> converted properly it seems, and all tests use either explicit name,
> or omit @XmlRootElement.
> That is, this looks like a bug in Jackson.
>
> I will add a Jira issue and get this fixed. As a work-around, you can
> either leave out the annotation (unless you have to use JAXB for XML);
> or add the name argument.
>

Would this be the same for @XmlElement(name="mappedName") ?

Does Jackson happen to have a factory to create a JSON configuration to
closely match XML representations?

I ask as I'm using rest-assured to perform testing of our JAX-RS interface,
querying entity.field for values. By default, entity does not exist (it's
not wrapped) and dates are represented as longs. Not sure yet what else
will prove a difference...

James