0

Given a .xsd file, I had the strategy to use a Maven tool to generate Java classes from this, and then use Jackson to serialize the data.

I'm using:

  • org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.13.3 to generate the Java classes
  • com.fasterxml.jackson.dataformat.xml.XmlMapper (2.9.2) with new JaxbAnnotationModule() to serialize

Example of generated code:

@XmlElement(required = true)
protected List<TLocalizedString> title;

Example of usage:

TLocalizedString tls = new TLocalizedString();
tls.setValue( string );
tls.setLocale( LOCALE );
item.getTitle().add( tls );

We are getting:

<item ....>
    <title>
        <title locale="en_US">The Title</title>
    </title>

What we expected:

<item ....>
    <title locale="en_US">The Title</title>

In other words the XML is coming out nested.

Is this something that should be controlled:

  • In the code at point of usage?
  • In the config for the Jackson serializer?
  • In the code generation via config or generation flags?
  • In the code generation via .xsd changes?
Stewart
  • 17,616
  • 8
  • 52
  • 80

1 Answers1

0

Finally found the answer. I had seen a lot of examples using this config, but had no idea what it was actually doing. It was this bug report which gave me the clue, via the phrase

wrapper element for Lists

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setDefaultUseWrapper( false ); // This prevents unnecessary nesting of XML elements
Stewart
  • 17,616
  • 8
  • 52
  • 80