Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB inheritance, unmarshal to subclass of marshaled class
    primarykey
    data
    text
    <p>I'm using JAXB to read and write XML. What I want is to use a base JAXB class for marshalling and an inherited JAXB class for unmarshalling. This is to allow a sender Java application to send XML to another receiver Java application. The sender and receiver will share a common JAXB library. I want the receiver to unmarshall the XML into a receiver specific JAXB class which extends the generic JAXB class.</p> <p>Example:</p> <p>This is the common JAXB class which is used by the sender.</p> <pre><code>@XmlRootElement(name="person") public class Person { public String name; public int age; } </code></pre> <p>This is the receiver specific JAXB class used when unmarshalling the XML. The receiver class has logic specific to the receiver application.</p> <pre><code>@XmlRootElement(name="person") public class ReceiverPerson extends Person { public doReceiverSpecificStuff() ... } </code></pre> <p>Marshalling works as expected. The problem is with unmarshalling, it still unmarshals to <code>Person</code> despite the JAXBContext using the package name of the subclassed <code>ReceiverPerson</code>.</p> <pre><code>JAXBContext jaxbContext = JAXBContext.newInstance(package name of ReceiverPerson); </code></pre> <p>What I want is to unmarshall to <code>ReceiverPerson</code>. The only way I've been able to do this is to remove <code>@XmlRootElement</code> from <code>Person</code>. Unfortunately doing this prevents <code>Person</code> from being marshaled. It's as if JAXB starts at the base class and works its way down until it finds the first <code>@XmlRootElement</code> with the appropriate name. I've tried adding a <code>createPerson()</code> method which returns <code>ReceiverPerson</code> to <code>ObjectFactory</code> but that doesn't help.</p>
    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