0
我有使用JAXB和對象列表的問題。 JAXB用於馬歇爾/解組個XML在春季4 類的結構沒有太大的XML結構,在地方,我使用ArrayList開發REST API使用ArrayList時JAXB無效的XML結構
我有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();
}
}
而解組:
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
但如果我發送給解組這樣的:
<Offer>
<clients>
<age>21</age>
</clients>
<decission>false</decission>
</Offer>
I得到:Offer.Client.age = 21 - 正確的價值。
據我所知,有些JAXB經驗,我做了幾件事情:
- 我試圖用註釋XMLSeeAlso
取得了客戶名單定製包裝類
@XmlRootElement @XmlAccessorType (XmlAccessType.FIELD) @XmlSeeAlso(Client.class) public class ClientsXMLWrapper {@@ xmlElement(name =「clients」) p rivate List客戶端;
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初始化:
- 的JAXBContext的JAXBContext = JAXBContext.newInstance(Offer.class,Client.class,ClientsXMLWrapper.class);
- JAXBContext jaxbContext = JAXBContext.newInstance(Offer.class,Client.class);
- JAXBContext jaxbContext = JAXBContext.newInstance(Offer.class,ClientsXMLWrapper.class);
沒有什麼幫助至今。你能幫我解決這個問題嗎? KOch。
完美的作品。問題解決了。謝謝 – KOch