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: Tue, 27 Nov 2012 07:59:43 +0000

Hi Tatu,

I have attached some sources to easily reproduce the issues I'm facing.
It is now a bit different from the original problem since I abstracted
Jersey away, for more simplicity.


   1. If you comment out Woodstox in the POM, you will be able to see these
   "weird" namespaces.
   They seem valid from a XML standard perspective, but extremely verbose
   and difficult to read, e.g. :

   <ArrayList>
       <zdef-1574603151:id
xmlns:zdef-1574603151="http://wwww.example.com/schema.xsd">ABC</zdef-1574603151:id>
       <zdef-1185908088:date
xmlns:zdef-1185908088="http://wwww.example.com/schema.xsd">2012-11-23</zdef-1185908088:date>
       <zdef-1530804123:timestamp
xmlns:zdef-1530804123="http://wwww.example.com/schema.xsd">2012-11-27T06:10:03Z</zdef-1530804123:timestamp>
       <zdef-1738378334:binary
xmlns:zdef-1738378334="http://wwww.example.com/schema.xsd">AQ==</zdef-1738378334:binary>
   </ArrayList>


   2. If you add Woodstox back, you would see the exceptions again:
   Unexpected IOException (of type java.io.IOException):
   javax.xml.stream.XMLStreamException: Trying to output second root,
   <ArrayList>


   3. In the attached sources, I use the JAXB maven plugin to generate the
   classes from src/main/resources/schema.xsd.
   There are also some "tweaks" using additional information in the schema
   and using *.xjb files, especially for the formatting of Joda DateTime.

   More importantly, the unit tests show the expected/desired output.
   e.g.
   - ["ABC","DEF"] V.S. {"id":["ABC","DEF"]}
   - insertion of empty namespaces for some reason

I hope this helps.

Cheers,

M.

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

> On Sat, Nov 24, 2012 at 1:43 PM, Marc CARRÉ <carre.marc_at_gmail.com> wrote:
> > 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).
>
> Ok.
>
> > However, either I get:
> >
> > with the default XML parser, weird / automatically generated namespaces
> for
> > my XML objects; or
>
> Weird in what sense? Unexpected prefixes (which would be aesthetic
> issue) or wrong namespace URI?
>
> > if I add Woodstox to the classpath, which seems to be the recommended
>
> Yes. Unfortunately there are some issues with Sjsxp (mostly not
> serious, but things like indentation can't be made to work), so
> Woodstox (or Aalto) is 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)
> >
> > 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)
>
> Of these, @JsonUnwrapped won't work (it's sort of orthogonal).
> XmlElementWrapper should work. Plain @XmlElement should work better
> than wrapping it in @XmlElements; handling of nesting may not be
> correctly (or completely) implemented. That might be something that
> could be improved, with a reproducible test case.
>
> >
> > 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?
>
> If you could share the POJO definition (generated from XSD), it would
> be easier to troubleshoot this. I understand that annotations must be
> ones that xjc (or whatever tool is used) will produce, and not
> Jackson-specific annotations.
>
> -+ Tatu +-
>
> >
> > 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 +-
> >
> >
>