users@jersey.java.net

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

From: Marc CARRÉ <carre.marc_at_gmail.com>
Date: Mon, 19 Nov 2012 07:02:53 +0000

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.

   1. XML would be fine, but JSON would be :
   [{"myID":"id1"},{"myID":"id2"}, ... ]
   2. JSON would be fine, but XML serialization would not work.

Any help would be greatly appreciated !

Cheers,

Marc.