How to use JAXBElement in jackson xml serialization?

782 views Asked by At

I have autogenerated java classes from xsd using xsd2java. I cannot modify neither xsd nor the java classes.

Problem: in one class an element of List<JAXBElement> is generated. If I now add any JAXBElement, the jackson xml marshaller won't show the proper xml element, but the properties of the JAXBElement serialized. Like declaredType, scope, etc. See below.

@XmlRootElement(name = "bookingRequest")
public class AutogeneratedReq {
    private List<JAXBElement<?>> someElements;
}

Usage:

AutogeneratedReq req = new AutogeneratedReq();
JAXBElement<?> person = new ObjectFactory().createPerson();
req.getSomeElements().add(person);

Result:

<someElements>
    <JAXBElement>
        <name>person</name>
        <declaredType>net.some.company.Person</declaredType>
        <scope>net.some.company</scope><value someattribues="test"/>
        <nil>false</nil>
        <globalScope>false</globalScope>
        <typeSubstituted>false</typeSubstituted>
    </JAXBElement>
</someElements>

Question: how can I tell jackson or spring-mvc to generate proper xml, and not JAXBElement serialization explicit?

1

There are 1 answers

0
Selim Ok On BEST ANSWER

I don't know which xsd2java utility you currently use, but you can try the following maven plugin to generate Java classes from XSD files.

https://github.com/highsource/jaxb2-basics/wiki/Using-JAXB2-Basics-Plugins

And then you can use following extension to create correctly typed POJO's.

https://github.com/highsource/jaxb2-basics/wiki/JAXB2-Simplify-Plugin

BUT even if you can create typed POJO attributes, the XML file generated from this POJO may not be 100% valid against original XSD file.


<jaxb:bindings multiple="true" node="//xs:element[@name='someElement']//xs:complexType//xs:choice//xs:element">
    <simplify:as-element-property/>
</jaxb:bindings>