如何在json中只打开id参数

uqdfh47h  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(106)

假设我们有这样的课程:

public class Cart{
   private int id;
   private String cartName;
   private String color;
   @JsonIgnore
   @OneToMany(mappedBy = "cart")
   private List<Item> items;
}
public class Item{
   @ManyToOne
   @JoinColumn(nullable = false)
   private Cart cart;
   private int id;
   private String itemName;
   private double weight;
}

通常,序列化item类时会看到:

{ 
   "id" : 1,
   "itemName" : "NiceItem",
   "weight" : 5.2,
   "cart" : {
      "id" : 3,
      "cartName" : "NiceCart",
      "color" : "Red"
   }
}

我的问题是如何使cart按其id序列化,如下所示:

{ 
   "id" : 1,
   "itemName" : "NiceItem",
   "weight" : 5.2,
   "cart_id" : 3
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题