0
這是我與JEE/REST應用程序的第一個項目XML到Java的ArrayList的支架應用
我的問題是,我不能讓我的服務器來解析XML後到我的對象至極包含一個ArrayList ...
這裏就是我處理@POST,服務器只返回它所得到:
Search.java
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Request returnRequest(Request input) {
return input;
}
當我張貼此:
<request>
<name>Salle 101</name>
<equipments>
<equipment>
<rfName>Tableau Blanc</rfName>
</equipment>
<equipment>
<rfName>Video Projecteur</rfName>
</equipment>
</equipments>
<startslot>9</startslot>
<endslot>10</endslot>
<size>6</size>
<site>Toulouse</site>
</request>
我得到這個:
<?xml version="1.0" encoding="UTF-8"?>
<request>
<endslot>10</endslot>
<equipments/>
<name>Salle 101</name>
<site>Toulouse</site>
<size>6</size>
<startslot>9</startslot>
</request>
正如你可以看到我沒有做我的設備清單正確返回...
這裏是我的實體文件:
Request.java
@XmlRootElement(name = "request")
public class Request {
//@XmlElement
private String name;
//@XmlElement
private Equipments equipments;
//@XmlElement
private int startSlot;
//@XmlElement
private int endSlot;
//@XmlElement
private int size;
//@XmlElement
private String site;
public Request(){
// my stuff
}
// getters & setters
Equipment.java
@Entity
@Table(name = "equipment")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Equipment.findAll", query = "SELECT e FROM Equipment e"),
@NamedQuery(name = "Equipment.findByRfName", query = "SELECT e FROM Equipment e WHERE e.rfName = :rfName")})
public class Equipment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "RF_NAME")
private String rfName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "equipment")
private Collection<RoomEquipment> roomEquipmentCollection;
public Equipment() {
}
public Equipment(String rfName) {
this.rfName = rfName;
}
//...
後搜索我的一點點看到創建ArrayList的一類可能是有用的,所以在這裏它是:
Equipments.java
@XmlRootElement(name="equipments")
public class Equipments implements Serializable {
private ArrayList<Equipment> equipments;
public Equipments(){
this.equipments = new ArrayList<>();
}
public void add(Equipment eq){
this.getEquipments().add(eq);
}
public ArrayList<Equipment> getEquipments(){
return this.equipments;
}
/**
* @param equipments the equipments to set
*/
public void setEquipments(ArrayList<Equipment> equipments) {
this.equipments = equipments;
}
}
感謝您的幫助