users@jersey.java.net

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

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Mon, 19 Nov 2012 10:30:59 -0800

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