java.lang.InstantiationException while mapping abstract class using jaxb

996 views Asked by At

I am following the blog post : http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html

and I have done all the same things, except that my subclasses are not public and are in the same file as that of the abstract class file. I get java.lang.InstantiationException exception javax.xml.bind.UnmarshalException: Unable to create an instance of org.apache.ambari.server.state.DependencyConditionInfo - with linked exception: [java.lang.InstantiationException] at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent( UnmarshallingContext.java:647)

EDIT

When I use @XmlSeeAlso I need to provide a default constructor, due to which I am not able to initialize the data member of the subclass.

Can anyone help me find a solution? Thanks

1

There are 1 answers

1
Luca Labate On

Why this is failing is because Jaxb will attempt to create an instance of User. which is abstract and therefore the failure.

On your abstract class add the annotations

@XmlTransient //Prevents the mapping of a JavaBean property/type to XML representation
@XmlSeeAlso({Admin.class, <other class>}) //Instructs JAXB to also bind other classes when binding this class

see the javadoc for each(XmlTransient, XmlSeeAlso)

What this will do is prevent jaxb from trying to initialize your abstract class.

The only downside to this method I found is there will be extra namespace information added to the xml that gets created.

Solved by @wyche5000