我有很多具有共同属性的实体。没有xml架构,所以我自己编写jaxb实体。
abstract class SuperEntity {
protected String id;
protected String name;
@XmlElement
public void setId() { .. sets Id .. }
@XmlElement
public void setName() { .. sets name .. }
}
// id and name are null after deserialization .. they are completely ignored
// there are other entities such as this, I don't want to repeat my code
@XmlRootElement
@XmlSeeAlso({SuperEntity.class})
class SpecificEntity extends SuperEntity {
protected String specificField;
@XmlElement
public void setSpecificField() { .. sets specific field .. }
}
SuperEntity根本没有反序列化(unmarshelled),将字段留空。如果我将字段和setter从超类复制到特定类,它可以工作,但我不想只是将该代码复制到每个子实体。谢谢你的帮助。
答案 0 :(得分:0)
当JAXB处理类模型时,它还将处理超类(未使用@XmlTransient
注释的类)。默认情况下,它不会处理子类。 @XmlSeeAlso
需要继承超类并引用子类。
答案 1 :(得分:0)
将班级定义更改为
@XmlRootElement
@XmlSeeAlso({SpecificEntity.class})
abstract class SuperEntity {
@XmlRootElement
class SpecificEntity extends SuperEntity {