I just started using JAXB to output XML data from Java objects. Polymorphism exists in my Java classes that don't seem to work in JAXB.
Below is an example of how I tried to deal with this, but in the output I did not expect a field: fieldA or fieldB.
@XmlRootElement(name = "root")
public class Root {
@XmlElement(name = "fieldInRoot")
private String fieldInRoot;
@XmlElement(name = "child")
private BodyResponse child;
}
public abstract class BodyResponse {
}
@XmlRootElement(name = "ResponseA")
public class ResponseA extends BodyResponse {
@XmlElement(name = "fieldA")
String fieldB;
}
@XmlRootElement(name = "ResponseB")
public class ResponseB extends BodyResponse {
@XmlElement(name = "fieldB")
String fieldB;
}
Before embarking on some complex inheritance development, is there a good approach to this?
smas source
share