JAXB在处理ArrayList时无效的XML结构
我遇到了使用JAXB和对象列表的问题。 JAXB用于从Spring 4开发的REST api中编组XML / unmarshall。类结构没有太多xml结构,在我使用ArrayList的地方
我有Java业务对象模型如下:
客户:
@XmlRootElement(name="client")
public class Client {
@XmlElement
public Integer age = Integer.valueOf(0);
public Client() {
super();
}
}
优惠(根元素):
@XmlRootElement
@XmlSeeAlso(Client.class)
public class Offer {
@XmlElement
public ArrayList<Client> clients = new ArrayList<Client>();
public Boolean decission = Boolean.FALSE;
public Offer() {
super();
}
}
和unmarshaller:
public static Offern unmarshalXMLOffer(String httpMessage) throws Exception{
logger.debug("unmarshal: receved data to unmarshal: " + httpMessage);
JAXBContext jaxbContext = JAXBContext.newInstance(Offer.class, Client.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(httpMessage);
Offer ca = (Offer)jaxbUnmarshaller.unmarshal(reader);
return ca;
}
问题:
当我发送:
<Offer>
<clients>
<client>
<age>21</age>
</client>
</clients>
<decission>false</decission>
</Offer>
我得到了: Offer.Client.age = 0
但是如果我发送给unmarshaller这个:
<Offer>
<clients>
<age>21</age>
</clients>
<decission>false</decission>
</Offer>
我得到了: Offer.Client.age = 21 - 正确的价值。
根据我的最佳知识和一些JAXB经验,我做了几件事情:
为客户列表创建自定义包装类
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD)@XmlSeeAlso(Client.class)公共类ClientsXMLWrapper {@XmlElement(NAME = “客户”)的私人客户名单;
public ClientsXMLWrapper(){
}
public ClientsXMLWrapper(List<Client> clientsList){
clients = clientsList;
}
public List<Client> getClients() {
return clients;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
}
我做了不同的JAXB初始化:
目前没有任何帮助。 你能帮我解决这个问题吗? 科赫。
尝试:
@XmlElementWrapper(name="clients")
@XmlElement(name="client")
public ArrayList<Client> clients = new ArrayList<Client>();
链接地址: http://www.djcxy.com/p/41863.html
上一篇: JAXB invalid XML structure while working with ArrayList