列表中的对象未转换为 XML



我有一个"Order"类,其中包含类"三元组"的对象列表。当我尝试获取 xml 格式的订单对象时,我正确地获取了除此之外的所有其他数据。以下是代码片段:

订货.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;
    }
}

三胞胎.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; }
}

订单资源.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>

怎样才能在电影中获得适当的价值?

编辑

问题是订单对象中的三元组类对象列表未转换为 XML。

为了解决这个问题,添加一个注释 - @XmlAccessorType(XmlAccessType.FIELD( 到三胞胎类工作,如dsp_user给出的答案的 EDIT 部分所述。

您在中没有获得任何值,因为 Order 类没有 films 属性/字段的 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));
}

我没有用您的数据尝试过这个,但我过去做过类似的事情,所以我知道它应该有效。

编辑:我现在已经尝试了您的代码,如果您只向三元组类添加一个注释,它就可以工作。

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

movies.add更改为 o.movies.add 。您正在修改由this引用保存的movies list,而在getObject方法中,您将一起返回不同的实例。

相关内容

  • 没有找到相关文章

最新更新