Skip to content
Advertisement

JAXB – Unmarshalling of abstract classes (InstantiationException)

I know, that it might seem that this question is a duplicate of this one: InstantiationException during JAXB Unmarshalling (abstract base class, with @XmlSeeAlso concrete sub class)

However, it is slightly different: We are using the maven-jaxb2-plugin to generate our Java-classes from xsd-files. You can find them here.

In our pom we are using the following configuration (we are using the jaxb-api in the version 2.3.1):

JavaScript

JAXB is generating the following classes:

Nachrichtenkopf.G2G2 (subclass):

JavaScript

Nachrichtenkopf.G2G (superclass):

JavaScript

Now, if we receive a XML without an xsi-type (see below) for the “Nachrichtenkopf” we get an InstantiationException since the unmarshaller is not able to create a concrete instance because of the missing type information:

JavaScript

So basically, I have 2 questions:

  1. Is there a way to tell the Unmarshaller how to unmarshal the message without the beforementioned type information?
  2. Is the xsi-type-information mandatory? Or in other words: is the XML from above valid according to the schemes (xsds)?

For the sake of completeness, the content of the externalBindings.xjb

JavaScript

Advertisement

Answer

Solved the issue by using a catalog-file. The catalog.cat-file (have a look at the first code-block in my original question) contains an entry, to advise the schema parser to look for the scheme that causes the problem in my local file-system rather than looking it up online. You can have a look at this post, if you are interested in rewriting online resources to local resources in the context of xsd-parsing (jaxb – how to map xsd files to URL to find them).

This way I can change the scheme which now resides in my src/main/resources folder. The scheme in question is http://www.osci.de/xinneres/basisnachricht/4/xinneres-basisnachricht.xsd. I changed name="Nachrichtenkopf.G2G" abstract="true" to name="Nachrichtenkopf.G2G" abstract="false" so that the class Nachrichtenkopf.G2G is no longer abstract and therefore, can be instantiated.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement