【发布时间】:2015-10-13 12:50:16
【问题描述】:
我有以下两个类层次结构:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="Dictionary",propOrder={"description"})
public class DictionaryDto implements Serializable, Descriptionable {
private static final long serialVersionUID = 1L;
@XmlAttribute(required=true)
private String id;
@XmlElement(name="Description")
private String description;
public DictionaryDto() {
}
public DictionaryDto(String id, String description) {
this.id = id;
this.description = description;
}
//getters and setters follows...
}
public class DictionaryProvinceDto extends DictionaryDto {
private String code;
private static final long serialVersionUID = 1L;
public DictionaryProvinceDto () {
super();
}
public DictionaryProvinceDto (String id, String description, String code) {
super(id, description);
this.code= code;
}
//getters and setters follows...
}
其中Descriptionable是一个接口,声明了唯一的方法String getDescription()
我正在尝试在 XML 中序列化 DictionaryProvinceDto 类型的对象,但我得到了
<Province id="030">
<code>AN</code>
</Province>
期望的输出:
<Province id="030">
<code>AN</code>
<description>my funny province</description>
</Province>
有人能解释为什么我没有得到我所期望的吗?
【问题讨论】:
-
如果您只想忽略该字段,请在
description字段顶部使用@XmlTransient注释 -
如果不想序列化,为什么要把
@XmlElement(name="Description")放在description前面呢? -
不,我希望对描述字段进行序列化。也许我对这一点不是很清楚
标签: java xml serialization jaxb