2
我有以下層次:MOXY JAXB @XmlID和繼承
public class Small {
private String xmlId;
@XmlID
@XmlAttribute
public String getXmlId() {
if (xmlId == null)
xmlId = "small" + new Random().nextInt();
return xmlId;
}
public void setXmlId(String id) {
this.xmlId = id;
}
}
public class Big extends Small {
// Code
}
當我試圖元帥類巴茲:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Baz {
private List<Small> smalls = new LinkedList<Small>();
private Small small;
private Big big;
@XmlIDREF
public Small getSmall() {
return small;
}
public void setSmall(Small small) {
this.small = small;
}
@XmlIDREF
public Big getBig() {
return big;
}
public void setBig(Big big) {
this.big = big;
}
@XmlElementWrapper(name = "smalls")
@XmlElement(name = "small")
public List<Small> getSmalls() {
return smalls;
}
public void setSmalls(List<Small> smalls) {
this.smalls = smalls;
}
}
我使用下面的測試代碼:
public class Test2 {
public static void main(String[] args) throws Exception {
Small s1 = new Small();
Small s2 = new Small();
Big b1 = new Big();
List<Small> smalls = new LinkedList<Small>();
smalls.add(s1);
smalls.add(s2);
smalls.add(b1);
Baz baz = new Baz();
baz.setSmalls(smalls);
baz.setSmall(s2);
baz.setBig(b1);
JAXBContext jc = JAXBContext.newInstance(Baz.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(baz, System.out);
}
}
不幸的是,在做元帥的時候,我面臨着以下例外:
Exception in thread "main" javax.xml.bind.JAXBException:
Exception Description: Invalid XmlIDREF on property [big]. Class [test.moxy.Big] is required to have a property annotated with XmlID.
我希望XmlID註釋可以被Big類繼承。我曾嘗試在Big類中添加「另一個」@XmlID註釋,它修復了編組問題。然而,這在生成XML模式時會導致另一個問題,該模式現在將包含兩個不允許的ID屬性。
我做錯了什麼?