简体   繁体   中英

JAXB - Should we bind classes with @XmlSeeAlso if they are fields of the same class

Lets have the following class:

@XmlType(name = "TestClass")
//@XmlSeeAlso({SomeClassOne.class, SomeClassTwo.class})
public class TestClass{

    @XmlElement
    private SomeClassOne someClassOne;

    @XmlElement
    private SomeClassTwo someClassTwo;

}

I try to serialize the object to XML using context and Marshaller. We have the following statement

context = JAXBContext.newInstance(TestClass.class);

The question is: Should I describe all binding classes of TestClass (which are SomeClassOne and SomeClassTwo) in the @XmlSeeAlso - @XmlSeeAlso({SomeClassOne.class, SomeClassTwo.class})

JAXB will automatically bind any mapped class contained by the class used to instantiate the JAXBContext .

@XmlSeeAlso is used for binding sub-classes.

An example from @XmlSeeAlso documentation :

@XmlSeeAlso({Dog.class,Cat.class})
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}

This enable an JAXBContext.newInstance(Animal.class) to correctly bind the Dog and Cat classes, despite the fact that they are never used in the Animal class.

So, in your exemple, it is not necessary to use @XmlSeeAlso to bind the SomeClassOne and SomeClassTwo classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM