I have a JAXB generated class, with a collection with the abstract type COBJECT.
COBJECT has multiple descendants, and these descendants also have abstract descendants too.
A snippet from the class hierarchy is as follows:
COBJECT (abstract)
|
|---CDEFINEDOBJECT
|
|--CDOMAINTYPE (abstract)
|
|---CCODEPHRASE
When I add an object of CCODEPHRASE to a collection with type COBJECT , such as
protected List<COBJECT> children;
JAXB attempts to create COBJECT, which is an abstract type, and it fails. I’ve tried to add
@XmlElementRefs({
@XmlElementRef(type = ARCHETYPEINTERNALREF.class),
@XmlElementRef(type = CONSTRAINTREF.class),
@XmlElementRef(type = CDEFINEDOBJECT.class),
@XmlElementRef(type = ARCHETYPESLOT.class),
@XmlElementRef(type = CCODEPHRASE.class)
})
protected List<COBJECT> children;
before children field, but I got “Type or any of its subclasses are not known ” exception in response.
The XML input contains XSI:TYPE= … attribute. How do I make JAXB handle this inheritance structure?
You need to ensure that the
JAXBContextis aware of the parent class and all the subtypes. This can be done by passing then all in when the JAXBContext is created.Or you can leverate an
@XmlSeeAlsoannotation. This lets you specify all the child classes so that you only need to inlcude the root when creating the JAXBContext:For More Information on JAXB and Inheritance