Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiationException during JAXB Unmarshalling (abstract base class, with @XmlSeeAlso concrete sub class)
    primarykey
    data
    text
    <p>I am running into JAXB Unmarshalling error as below. The foo.bar.Base is an abstract class, with an @XmlSeeAlso annotation, which lists foo.bar.SubBase (which is a concrete subclass of foo.bar.Base)</p> <p>Both of the above classes are statically reachable from a main/entry class: com.example.Request</p> <p>The JAXBContext is create using the packages string variant viz:</p> <pre><code>JAXBContext.newInstance("com.example",...); </code></pre> <p>The above created JAXBContext correctly lists all the three classes : <code>com.example.Request, foo.bar.Base and foo.bar.SubBase as "classes known to this JAXBContext"</code></p> <p>But it fails at runtime during the unmarshal call below.. I am unable to figure out what is wrong here. </p> <pre><code>unmarshaller.unmarshal(&lt;some-DOM-Element-Instance&gt;, com.example.Request.class); </code></pre> <p>Any pointers will be appreciated! Thanks!</p> <p>The stacktrace is:</p> <pre><code> Caused by: javax.xml.bind.UnmarshalException: Unable to create an instance of foo.bar.Base - with linked exception: [java.lang.InstantiationException] at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642) at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:609) at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:181) at com.sun.xml.bind.v2.runtime.unmarshaller.XsiTypeLoader.startElement(XsiTypeLoader.java:76) at com.sun.xml.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:55) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:481) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459) at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71) at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148) at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:239) at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:276) at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:245) at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:122) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:314) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:293) Caused by: java.lang.InstantiationException at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:123) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:261) at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.createInstance(UnmarshallingContext.java:603) ... 69 more </code></pre> <p>EDIT @Blaise and @Ross</p> <p>Thank you very much Blaise and Ross for your pointers. I think I should have included the schema that is being worked off, here. The relevant schema looks like this:</p> <pre><code> &lt;xs:complexType name="Request"&gt; &lt;xs:sequence&gt; &lt;xs:element name="selectedBase" form="unqualified" nillable="true" type="xs:anyType" minOccurs="0"/&gt; &lt;xs:element name="selectedSubBase" form="unqualified" nillable="true" type="ns1:SubBase" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="Base"&gt; &lt;xs:sequence&gt; &lt;xs:element name="ID" form="unqualified" nillable="true" type="xs:string" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="SubBase"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="ns1:Base"&gt; &lt;xs:sequence&gt; &lt;xs:element name="subBaseElement" form="unqualified" nillable="true" type="xs:anyType" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; </code></pre> <p>So the schema doesn't have substitution group definition (so I guess <code>@XmlElementRef</code> doesn't apply here, or would it still work?), but is using extension. The payload will be :</p> <pre><code>&lt;ns:Request&gt; &lt;selectedBase&gt;123&lt;/selectedBase&gt; &lt;selectedSubBase&gt; &lt;ID&gt;321&lt;/ID&gt; &lt;subBaseElement&gt;123&lt;/subBaseElement&gt; &lt;/selectedSubBase&gt; &lt;/ns:Request&gt; </code></pre> <p>So , the element in the payload occuring is <code>&lt;selectedSubBase&gt;</code> and not <code>&lt;selectedBase xsi:type="ns:SubBase"/&gt;</code></p> <p>So which strategy would apply here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload