Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB annotation for class hierarchy

Tags:

java

xml

jaxb

Hey, I have 2 classes. When I'm trying to create an XML structure from them, I only get the root element (A). Why? Am I using 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(){
    }
}    
like image 203
Udi Avatar asked May 17 '11 12:05

Udi


People also ask

What is @XmlAccessorType Xmlaccesstype field?

@XmlAccessorType. Package, Class. Defines the fields and properties of your Java classes that the JAXB engine uses for binding. It has four values: PUBLIC_MEMBER , FIELD , PROPERTY and NONE .

What is XmlAccessorType?

Annotation Type XmlAccessorTypeControls whether fields or Javabean properties are serialized by default. Usage. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.

What is @XmlTransient?

The @XmlTransient annotation is useful for resolving name collisions between a JavaBean property name and a field name or preventing the mapping of a field/property. A name collision can occur when the decapitalized JavaBean property name and a field name are the same.


1 Answers

You probably need to use @XmlSeeAlso annotation in your top class:

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

I wrote 'probably', because it depends on how do you set up your JAXB context. Basically you need to make sure all the classes which are supposed to be serialized are known to JAXB. If your B class is not mentioned anywhere else (e.g. as a property type of one of classes which are already known to JAXB), then JAXB has no chance to know how to serialize instances of B. The intention of @XmlSeeAlso annotation is to make sure JAXB looks into these listed classes too.

UPDATE:

Alternatively you can provide the list of all the subclasses when creating the JAXBContext object using JAXBContext.newInstance(Class...), e.g.:

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

instead of

   JAXBContext.newInstance(A.class);

which you probably already do.

But my opinion this is a worse solution, because it makes you think of related classes every time you use JAXB in your code. In the top solution you set the relations once forever.

like image 114
Grzegorz Oledzki Avatar answered Oct 02 '22 18:10

Grzegorz Oledzki