我有一个看起来像这样的简单类
import nl.hu.pf.coproco.model.Observeable;
@XmlRootElement (name = "Purpose")
@XmlAccessorType (XmlAccessType.FIELD)
public class Purpose implements Observeable{
private String name;
private ArrayList<Observer> observers;
@XmlElement (name = "subPurpose")
private ArrayList<Purpose> subPurposes;
//methods
}
但Observable是一个接口,所以我得到一个例外,因为JAXB无法处理接口。我试图找出如何忽略ArrayList<Observer>
字段,以便它不会导出到xml。
我尝试使用@XmlTransient
注释,但我得到以下感知javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"purpose"). Expected elements are <{}Purpose>
这种情况发生在Unmarshalling,但我也想要马歇尔那个班级
示例xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purpose>
<name>All Purposes</name>
<subPurpose>
<name>Creational</name>
</subPurpose>
<subPurpose>
<name>Structural</name>
</subPurpose>
<subPurpose>
<name>Behavioral</name>
<subPurpose>
<name>Behavioral 1</name>
</subPurpose>
<subPurpose>
<name>Behavioral 2</name>
</subPurpose>
</subPurpose>
</purpose>
界面Observable
如下所示:
public interface Observeable {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers(Object o, String arg);
}
答案 0 :(得分:3)
您的xml包含purpose
而不是Purpose
,因此将@XmlRootElement(name = "Purpose")
更改为
@XmlRootElement(name = "purpose")
在像这样的观察者身上添加@XmlTransient
@XmlTransient
private ArrayList<Observer> observers;