英文:
Spring Many to one in json
问题
{
"id": 2,
"price": 300.00,
"discount": 35.00,
"product": {
"id": 2,
"title": "dsfa",
"description": "dsfa"
// ... other properties of the product entity
}
}
想要将产品添加到 JSON 响应中,你可以按照上面的格式进行调整。
英文:
Hello is it possible to add mapped value in json?
Product entity
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String title;
private String description;
@OneToMany(fetch = FetchType.LAZY)
private List<Options> options= new ArrayList<>();
Option entity
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Product product;
private BigDecimal price=new BigDecimal(0);
private BigDecimal discount=new BigDecimal(0);
What i get is
{
"id": 2,
"price": 300.00,
"discount": 35.00
},
What i want is
{
"id": 2,
"price": 300.00,
"discount": 35.00,
"product":[
"id": 2,
"title": "dsfa",
"description": "dsfa",
....
]
},
I want to add product to json response, how can i achieve this?
答案1
得分: 1
一个产品有许多选项或反之亦然?
在您的代码中,一个产品有许多选项。
但如果关系正确的话,获得自定义结果的更好方式是使用DTO(数据传输对象)。
通过DTO,您可以创建一个包含所有所需细节的自定义类,示例如下:
ProductDTO
private Integer id;
private String title;
private String description;
getter / setter
OptionDTO
private Integer id;
private BigDecimal price = new BigDecimal(0);
private BigDecimal discount = new BigDecimal(0);
private List<Product> products;
getter / setter
因此,首先使用您的服务和仓库检索数据,然后设置DTO。
然后,您可以发送数据并获取您的自定义JSON。
英文:
One Product have many Option or viceversa?
In your code one Product have many Option.
But if the relation is correct, the better way to get a custom result is with DTOs.
With DTO you can create a custom Class with all the details you need, example:
ProductDTO
private Integer id;
private String title;
private String description;
getter / setter
OptionDTO
private Integer id;
private BigDecimal price=new BigDecimal(0);
private BigDecimal discount=new BigDecimal(0);
private List<Product> products;
getter / setter
Soo, first retrieve with you service and reposity you data, then you set the DTO.
After you can send you data and get your custom Json.
答案2
得分: 1
是的,只需使用@JsonIgnoreProperties
。
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String title;
private String description;
@OneToMany(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = {"product"})
private List<Options> options = new ArrayList<>();
第二个类:
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties(value = {"options"})
private Product product;
private BigDecimal price = new BigDecimal(0);
private BigDecimal discount = new BigDecimal(0);
这样可以避免JSON结果中的无限循环,并获取所有引用对象(关联关系)。
英文:
Yes, just use @JsonIgnoreProperties
.
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String title;
private String description;
@OneToMany(fetch = FetchType.LAZY)
@JsonIgnoreProperties(value = {"product"})
private List<Options> options= new ArrayList<>();
and second class:
@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties(value = {"options"})
private Product product;
private BigDecimal price=new BigDecimal(0);
private BigDecimal discount=new BigDecimal(0);
You will avoid the infinity loop in json result and get all reference objects (relationships).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论