I'm trying to produce something like this with JAXB:
Foo
Bar
The child elements of all stem from a common super-class.
In Java it's like this:
@XmlRootElement(name = "person")
public class Person {
public String firstName;
public String lastName;
@XmlElementWrapper(name = "identities")
public Set identities = new HashSet();
}
Where Identity is a super class for Blue, Green and some others.
public class Identity {
@XmlID
@XmlAttribute
public String id;
}
@XmlRootElement(name = "blue")
public class Blue extends Identity {
public String oneOfManyFields;
}
@XmlRootElement(name = "green")
public class Green extends Identity {}
How do I properly annotate the classes to get what I need? Currently, the output is like so:
解决方案
Simply modify your example to use the @XmlElementRef annotation on the identities property.
import java.util.HashSet;
import java.util.Set;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "person")
public class Person {
public String firstName;
public String lastName;
@XmlElementWrapper(name = "identities")
@XmlElementRef
public Set identities = new HashSet();
}