【发布时间】:2014-01-03 16:26:31
【问题描述】:
我有 2 个类,一个扩展另一个。超类正确编组,但添加一个属性的子类没有。 XML 中不存在额外属性。
超类:
@XmlRootElement()
@XmlAccessorType(XmlAccessType.NONE)
public class SessionRecord extends Record {
SimpleDateFormat hhmm = new SimpleDateFormat("HH:mm");
SimpleDateFormat day = new SimpleDateFormat("EEEEE");
@XmlAttribute protected int sessionId;
@XmlAttribute protected boolean open;
@XmlAttribute protected boolean night;
protected Date start;
protected Date finish;
protected boolean setup;
protected boolean takedown;
@XmlAttribute
public String getDescription() {
if (start==null) start = new Date();
if (finish==null) finish = new Date();
return day.format(start)+(night ? " Night " : " ")+hhmm.format(start)+"-"+hhmm.format(finish)+" "+type();
}
private String type() {
return setup ? "Setup" : (open ? "Open" : (takedown ? "Takedown" : ""));
}
@XmlAttribute
public boolean isSetupTakedown() {
return setup || takedown;
}
}
这会产生类似这样的 XML 元素:
<sessionRecord setupTakedown="true" description="Saturday 09:00-13:00 Setup" night="false" open="false" sessionId="0"/>
没关系。
但是子类:
@XmlRootElement()
public class VolunteerSession extends SessionRecord {
@XmlAttribute private boolean available;
}
产生相同的输出,available 属性未编组。为什么 JAXB 不编组额外属性?
编辑
更多信息:
记录超类只是这样:
public abstract class Record {}
这是代表顶级文档元素的类。它包含记录列表:
@XmlRootElement(name="response")
@XmlSeeAlso({
RecordList.class,
VolunteerAssignment.class,
VolunteerRecord.class,
SessionRecord.class,
VolunteerSession.class,
VolunteerArea.class,
PossibleAssignment.class})
public class XMLResponse {
@XmlAttribute private String errorMessage;
private List<RecordList<? extends Record>> recordLists = new ArrayList<RecordList<? extends Record>>();
//snip...
public void setError(String errorMessage) {
this.errorMessage = errorMessage;
}
@XmlMixed
public List<RecordList<? extends Record>> getRecordLists() {
return recordLists;
}
}
最后是记录列表
@XmlRootElement()
public class RecordList<T extends Record> {
@XmlAttribute private String name;
@XmlAttribute private int total;
@XmlAttribute private int count;
@XmlAttribute private int start;
@XmlAttribute private boolean update;
private List<T> records;
// snip constructors, setters
@XmlMixed
public List<T> getRecords() {
return records;
}
}
【问题讨论】:
-
尝试在您的 VolunteerSession 类中添加可用的 getter 和 setter。
-
试过了,没区别
-
您能否添加您的
Record课程以及如何创建您的JAXBContext? -
根据要求添加了额外信息。实际上@XmlSeeAlso 是在 XMLResponse 中,而不是我之前说的 Record,但这不应该造成太大的差异
标签: java inheritance jaxb