[英]Inheritance with JAXB (unmarshalling)
我有许多具有共同属性的实体。 没有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完全不反序列化(不带壳),使字段为空。 如果我将字段和设置器从超类复制到特定类,则可以,但是我不想仅将代码复制到每个子实体。 谢谢您的帮助。
当JAXB处理类模型时,它还将处理超类(未使用@XmlTransient
注释的超类)。 默认情况下,它不会处理子类。 @XmlSeeAlso
需要继续超类并引用子类。
将类定义更改为
@XmlRootElement
@XmlSeeAlso({SpecificEntity.class})
abstract class SuperEntity {
@XmlRootElement
class SpecificEntity extends SuperEntity {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.