users@jersey.java.net

[Jersey] Re: List of primitive types and formatting in both JSON and XML (with XSD)

From: Marc CARRÉ <carre.marc_at_gmail.com>
Date: Sat, 24 Nov 2012 21:43:17 +0000

Hi Tatu,

Thanks for the suggestion.
I have played with the new version of Jackson / FasterXML and migrated my
project to version 2.1.1.
(Obviously, I removed Jackson 1.* that I was using for JSON previously, and
updated all dependencies to be at this version).

However, either I get:

   - with the default XML parser, weird / automatically generated
   namespaces for my XML objects; or

   - if I add Woodstox to the classpath, which seems to be the recommended
   practice with Jackson / FasterXML:
   Unexpected IOException (of type java.io.IOException):
   javax.xml.stream.XMLStreamException: Trying to output second root,
   <ArrayList>
   (see also issue #38 on
jackson-dataformat-xml<https://github.com/FasterXML/jackson-dataformat-xml/issues/38>
   )

I have also tried several combinations with the below annotations, without
any luck so far:
    @XmlElements({ @XmlElement(name = "id") })
    @XmlElementWrapper(name = "ids")
    @JsonUnwrapped(enabled = true)

Also, changing the below parameter to false or true, doesn't seem to change
anything for XML serialization:
        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(false);
        XmlMapper xmlMapper = new XmlMapper(module);

Any other suggestion?
Anything I might be doing incorrectly?

Cheers,

M.


2012/11/19 Tatu Saloranta <tsaloranta_at_gmail.com>

> On Sun, Nov 18, 2012 at 11:02 PM, Marc CARRÉ <carre.marc_at_gmail.com> wrote:
> > Hi,
> >
> > I'm looking to produce a simple List<T> (List<String> in that case, but I
> > would also need several other primitive types) and :
> >
> > have it described in my XSD, e.g. :
> >
> > <xs:complexType name="MyID">
> > <xs:sequence>
> > <xs:element name="myID" type="xs:string" />
> > </xs:sequence>
> > </xs:complexType>
> >
> > produce nicely formatted JSON (just an array of string) :
> >
> > ["id1", "id2", ... ]
> >
> > produce nicely formatted XML (respect the naming conventions from the
> > schema) :
> >
> > <myIDs>
> > <myID>id1</myID>
> > <myID>id2</myID>
> > ...
> > </myIDs>
> >
> > However I have found it impossible, so far, to combine all these 3
> features
> > in Jersey.
> > I tried:
> >
> > List<JAXBElement<String>>
> > Generating an IDs container and annotating in various ways
> (@XmlRootElement
> > for the class, @XmlElement and @JsonUnwrapped for the list itself)
> > Set JSONConfiguration.FEATURE_POJO_MAPPING to true
> >
> > but didn't find a solution : I only manage to have two out of the three
> > features.
> >
> > e.g.
> >
> > XML would be fine, but JSON would be : [{"myID":"id1"},{"myID":"id2"},
> ... ]
> > JSON would be fine, but XML serialization would not work.
> >
> > Any help would be greatly appreciated !
>
> Have you tried Jackson XML module
> (https://github.com/FasterXML/jackson-dataformat-xml) for XML? I am
> not 100% sure if it'd work this way, but I think this would work.
> Note that you could use Jackson XML annotations
> (@JacksonXmlElementWrapper etc) to avoid affecting JSON output there.
>
> Alternatively, if this is not an option, you could either disable
> Jackson JAXB annotation support (by creating JacksonJaxrsJsonProvider
> manually, without JAXB annotations enabled), or modify it by
> overriding method(s) that use wrapper to change property names.
>
> -+ Tatu +-
>