JAXB annotations for class hierarchy

Hey, I have 2 classes. When I try to create an XML structure from them, I get only the root element (A). What for? Am I using the wrong annotations?

@XmlRootElement(name = "a")
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class A{
    @XmlElement
    int a;

    protected A(){
    }
 }

@XmlAccessorType(XmlAccessType.FIELD)
public class B extends A{
    @XmlElement
    int b;

    protected B(){
    }
}    
+5
source share
1 answer

You probably need to use annotation @XmlSeeAlsoin your upper class:

@XmlSeeAlso(B.class)
@XmlRootElement(name = "a")
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class A{

"", , JAXB-. , , , JAXB. B (, , JAXB), JAXB , B. @XmlSeeAlso , , JAXB .

UPDATE:

JAXBContext JAXBContext.newInstance(Class...), :

   JAXBContext.newInstance(A.class, B.class);

   JAXBContext.newInstance(A.class);

, , .

, , JAXB . .

+7

All Articles