简体   繁体   中英

java.lang.InstantiationException while mapping abstract class using jaxb

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM