我有一个'Order'类,其中包含类'Triplet'的对象列表。 当我尝试以xml格式获取订单对象时,我会正确地获取所有其他数据。以下是代码段:
Order.java:
@XmlRootElement
public class Order {
private String orderid;
private String orderStatus;
private String paidStatus;
private String address;
private double total;
private String emailid;
private String username;
private List<Triplet> movies=new ArrayList<>();
public Order(String orderid, String orderStatus, String paidStatus, String address, double total, String emailid,
String username) {
super();
this.orderid = orderid;
this.orderStatus = orderStatus;
this.paidStatus = paidStatus;
this.address = address;
this.total = total;
this.emailid = emailid;
this.username = username;
}
public Order(String orderid, String orderStatus, String paidStatus, String address, double total, String emailid,
String username, List<Triplet> movies) {
super();
this.orderid = orderid;
this.orderStatus = orderStatus;
this.paidStatus = paidStatus;
this.address = address;
this.total = total;
this.emailid = emailid;
this.username = username;
this.movies = movies;
}
public Order() {
super();
}
//getters and setters
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
public String getPaidStatus() {
return paidStatus;
}
public void setPaidStatus(String paidStatus) {
this.paidStatus = paidStatus;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<Triplet> getMovies() {
return movies;
}
public void setMovies(List<Triplet> movies) {
this.movies = movies;
}
public Order getObject(){
Order o=new Order();
o.setAddress("address1");
o.setEmailid("email");
o.setOrderid("orderid");
o.setOrderStatus("in process");
o.setPaidStatus("n");
o.setTotal(250.5);
o.setUsername("admin");
movies.add(new Triplet(1L,2,200.0));
movies.add(new Triplet(2L,3,150.0));
o.setMovies(movies);
return o;
}
}
Triplet.java:
@XmlRootElement
public class Triplet implements Serializable {
private long movieid;
private int quantity;
private double price;
public Triplet() {
super();
}
public Triplet(long movieid, int quantity, double price) {
this.movieid = movieid;
this.quantity = quantity;
this.price = price;
}
public long getMovieid() { return movieid; }
public int getQuantity() { return quantity; }
public double getPrice() { return price; }
}
OrderResource.java:
@Path("/orders")
public class OrderResource {
OrderService os=new OrderService();
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/{emailid}")
public Order getOrderByCustomer(@PathParam("emailid") String emailid){
Order o=new Order();
return o.getObject();
}
}
输出:
<order>
<address>address1</address>
<emailid>email</emailid>
<movies/>
<movies/>
<orderStatus>in process</orderStatus>
<orderid>orderid</orderid>
<paidStatus>n</paidStatus>
<total>250.5</total>
<username>admin</username>
</order>
如何在电影中获得正确的价值?
修改
问题是Order对象中的Triplet类对象列表没有转换为XML。
要解决此问题,请在Triplet类中添加注释 - @XmlAccessorType(XmlAccessType.FIELD),如dsp_user给出的答案的EDIT部分所述。
答案 0 :(得分:0)
将movies.add
更改为o.movies.add
。您正在修改由list
引用保留的movies
this
,而在getObject
方法中,您将同时返回另一个实例。
答案 1 :(得分:0)
您未在&lt; movies&gt;中获取任何值因为Order类没有movie属性/字段的getter。将您的Order类更改为
@XmlRootElement
public class Order {
...
@XmlElement
private List<Triplet> movies;
public List<Triplet> getMovies(){
if(movies == null)
movies = new ArrayList<Triplet>();
return movies;
}
....
//The setter for movies isn't necessary because you can add new movies
//by calling getMovies().add(new Movie(1L,2,300.0));
}
我没有尝试过您的数据,但我过去做过类似的事情,所以我知道它应该有效。
编辑:我现在已经尝试过你的代码了,如果你只是在Triplet类中添加一个注释就可以了。 @XmlAccessorType(XmlAccessType.FIELD)
public class Triplet {
...
}
我测试了这段代码(注释和未注释)
final String ORDER_XML = "C:\\test\\Order.xml";
JAXBContext context = JAXBContext.newInstance(Order.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Order o = new Order();
/* o.setAddress("address1");
o.setEmailid("email");
o.setOrderid("orderid");
o.setOrderStatus("in process");
o.setPaidStatus("n");
o.setTotal(250.5);
o.setUsername("admin");
o.getMovies().add(new Triplet(1L,2,200.0));
o.getMovies().add(new Triplet(2L,3,150.0));
o.getMovies().add(new Triplet(3L,3,400.0));*/
//o.setMovies(movies);
m.marshal(o.getObject(), new File(ORDER_XML));
生成的XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
<address>address1</address>
<emailid>email</emailid>
<movies>
<movieid>1</movieid>
<quantity>2</quantity>
<price>200.0</price>
</movies>
<movies>
<movieid>2</movieid>
<quantity>3</quantity>
<price>150.0</price>
</movies>
<orderStatus>in process</orderStatus>
<orderid>orderid</orderid>
<paidStatus>n</paidStatus>
<total>250.5</total>
<username>admin</username>
</order>