JAXB invalid XML structure while working with ArrayList
I have issue working with JAXB, and list of objects. JAXB is used to marshall/unmarshall XMLs from REST api developed in Spring 4. The class structure doesn't much xml structure, in place where I use ArrayList
I have Java Business Object Model as follows:
Client:
@XmlRootElement(name="client")
public class Client {
@XmlElement
public Integer age = Integer.valueOf(0);
public Client() {
super();
}
}
Offer (root element):
@XmlRootElement
@XmlSeeAlso(Client.class)
public class Offer {
@XmlElement
public ArrayList<Client> clients = new ArrayList<Client>();
public Boolean decission = Boolean.FALSE;
public Offer() {
super();
}
}
and 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;
}
The issue:
When I send:
<Offer>
<clients>
<client>
<age>21</age>
</client>
</clients>
<decission>false</decission>
</Offer>
I got: Offer.Client.age = 0
but if i send to unmarshaller this:
<Offer>
<clients>
<age>21</age>
</clients>
<decission>false</decission>
</Offer>
I got: Offer.Client.age = 21 - right value.
According to my best knowledge and some JAXB experience i did few things:
made custom wrapper class for client list
@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso(Client.class) public class ClientsXMLWrapper { @XmlElement(name = "clients") private List clients;
public ClientsXMLWrapper(){
}
public ClientsXMLWrapper(List<Client> clientsList){
clients = clientsList;
}
public List<Client> getClients() {
return clients;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
}
i did different JAXB initializations:
Nothing helped so far. Could you please help me resolve that issue? KOch.
尝试:
@XmlElementWrapper(name="clients")
@XmlElement(name="client")
public ArrayList<Client> clients = new ArrayList<Client>();
链接地址: http://www.djcxy.com/p/41864.html
上一篇: SQL中EXISTS和IN之间的区别?