简体   繁体   English

列表中的对象未转换为XML

[英]Objects in the list not being converted to XML

I have an 'Order' class which contains a list of objects of class 'Triplet'. 我有一个“ Order”类,其中包含“ Triplet”类的对象列表。 When I try to get an order object in xml format I get all other data properly except this. 当我尝试以xml格式获取订单对象时,除此以外,我会正确获取所有其他数据。 Following is the code snippet: 以下是代码段:

Order.java: 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: 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: 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();
}
}

Output: 输出:

<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>

What can be done to get proper values in movies? 怎样才能在电影中获得适当的价值?

Edit 编辑

The problem was that the list of Triplet class object in Order object was not getting converted to XML. 问题在于Order对象中的Triplet类对象的列表未转换为XML。

To solve this problem adding an annotation - @XmlAccessorType(XmlAccessType.FIELD) to Triplet class works as mentioned in the EDIT part of the answer given by dsp_user. 若要解决此问题,请按照dsp_user给出的答案的EDIT部分中所述,将注释-@XmlAccessorType(XmlAccessType.FIELD)添加到Triplet类。

Change movies.add to o.movies.add . 更改movies.addo.movies.add You are modifying the list of movies that is held by this reference whereas in getObject method you are returning a different instance all together. 您正在修改this引用所保存的movies list ,而在getObject方法中,您将一起返回另一个实例。

You're not getting any values in <movies> because the Order class has no getter for the movies property/field. 您没有在<movies>中获得任何值,因为Order类没有movie属性/字段的getter。 Change your Order class to something like 将您的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));

}

I haven't tried this with your data but I've done something similar in the past so I know it should work. 我没有用您的数据尝试过此操作,但是过去我做过类似的事情,所以我知道它应该可以工作。

EDIT: I've now tried your code and it works if you just add one annotation to the Triplet class. 编辑:我现在已经尝试过您的代码,并且如果您仅向Triplet类添加一个注释,它就会起作用。

 @XmlAccessorType(XmlAccessType.FIELD)
 public class Triplet  {
 ...
 }

I tested with this code (both the commented and uncommented) 我对此代码进行了测试(已注释和未注释)

 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));

The generated 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM