Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB throwing InstantiationException trying to marshal xsi:type based on abstract class
    primarykey
    data
    text
    <p>I am having an issue with using inheritance and JAXB unmarshalling. I have read the number of examples (specifically a much-reference blog at <a href="http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html" rel="nofollow noreferrer">http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html</a> and a very similar SO question here: <a href="https://stackoverflow.com/questions/11219074/jaxb-xsitype-subclass-unmarshalling-not-working">JAXB xsi:type subclass unmarshalling not working</a>) and am still having difficulties.</p> <p>Like many of these other questions, I am trying to create an XML representation of an object whose fields rely on subclasses for information. I do not know at compile time what the concrete subclass implementation will be, so things like XmlSeeAlso is not really available. </p> <p>In my test case I have a Root class with an abstract class (A) that has a concrete subtype (B):</p> <pre><code>@XmlRootElement @ToString public class Root { private A theClass; public A getTheClass() { return theClass; } public void setTheClass(A theClass) { this.theClass = theClass; } } @ToString public abstract class A { } @XmlRootElement @ToString public class B extends A { private String b = "from B-" + System.currentTimeMillis() public String getB() { return b; } public void setB(String b) { this.b = b; } } </code></pre> <p>Where @ToString are annotations from project Lombok.</p> <p>I can marshall with no problem:</p> <pre><code>@Test public void marshallTest() { try { Root r = new Root(); B b = new B(); r.setTheClass(b); Class&lt;?&gt;[] classArray = new Class&lt;?&gt; [] { Root.class, B.class }; JAXBContext context = JAXBContext.newInstance(classArray); Marshaller marshaller = context.createMarshaller(); try (FileWriter fw = new FileWriter(JAXB_TEST_XML)) { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(r, fw); } try(StringWriter sw = new StringWriter() ) { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(r, sw); log.debug("{}", sw.toString()); } } catch (IOException | JAXBException e) { e.printStackTrace(); fail(e.getMessage()); } } </code></pre> <p>Which produces the following xml:</p> <pre><code>&lt;root&gt; &lt;theClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"&gt; &lt;b&gt;from B-1375211003868&lt;/b&gt; &lt;/theClass&gt; &lt;/root&gt; </code></pre> <p>When I try to unmarshall (using MOXY JAXB implemntation) I get: </p> <pre><code>This class does not define a public default constructor, or the constructor raised an exception. Internal Exception: java.lang.InstantiationException Descriptor: XMLDescriptor(xml.A --&gt; []) </code></pre> <p>With the following code:</p> <pre><code>@Test public void unmarshall() { try { JAXBContext context = JAXBContext.newInstance(new Class&lt;?&gt;[] {Root.class, A.class}); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder db = documentBuilderFactory.newDocumentBuilder(); Root r = null; try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(JAXB_TEST_XML))) { Document doc = db.parse(bis); Unmarshaller unmarshaller = context.createUnmarshaller(); JAXBElement&lt;?&gt; result = unmarshaller.unmarshal(doc, Root.class); r = (Root) result.getValue(); } assertTrue(r != null &amp; r.getTheClass() != null &amp;&amp; r.getTheClass() instanceof B); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } } </code></pre> <p>I have tried making the unmarshalling namespace aware (as with <a href="https://stackoverflow.com/questions/11219074/jaxb-xsitype-subclass-unmarshalling-not-working)a">JAXB xsi:type subclass unmarshalling not working</a> which has not worked. I have tried using XmlElementRef which does not work either. I have tried the latest glassfish api and implementations downloaded from maven central (2.2.8). I have tried the MOXY eclipse persistence JAXB implementation. Neither have worked. I have tried unmarshalling without using a document builder which does not work as well. I have tried passing the Root.class and A.class into the JAXB context which also does not work. </p> <p>I have a feeling that I have a fundamental misconception about what is going on. Any hints or ideas would be appreciated. Thank you.</p> <ul> <li>Chooks</li> </ul>
    singulars
    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.
    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