春季 JSON 中的多对一

huangapple go评论109阅读模式
英文:

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&lt;Product&gt; 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 = {&quot;product&quot;})
private List&lt;Options&gt; options= new ArrayList&lt;&gt;();

and second class:

@Id
@Column(unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties(value = {&quot;options&quot;})
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).

huangapple
  • 本文由 发表于 2020年8月17日 01:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63439957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定