gpt4 book ai didi

java - JSON 在发布时递归调用对象

转载 作者:太空宇宙 更新时间:2023-11-04 11:10:18 24 4
gpt4 key购买 nike

我正在通过 Swagger 测试我的应用程序,当尝试发布一个包含其他对象列表的对象时,我收到错误 java.lang.StackOverflowError。基本上,我正在尝试编写一张发票,以 Swagger 形式表示为 JSON,在填写发票数据时,我注意到发票项目包含发票的重复字段。

{
"date": "2017-08-27",
"counterparty": {
"address": {
"houseNumber": "string",
"streetName": "string",
"townName": "string",
"zipCode": "string"
},
"bankName": "string",
"bankNumber": "string",
"companyName": "string",
"nip": "string",
"phoneNumber": "string"
},
"invoiceItems": [
{
"amount": 0,
"description": "string",
"id": 0,
"invoice": { //SHOULD NOT BE HERE
"id": 0,
"date": "2017-09-07",
"counterparty": {
"address": {
"houseNumber": "string",
"streetName": "string",
"townName": "string",
"zipCode": "string"
},
"bankName": "string",
"bankNumber": "string",
"companyName": "string",
"nip": "string",
"phoneNumber": "string"
},
"invoiceItems": [
{}
]
},
"numberOfItems": 0,
"vat": "VAT_23",
"vatAmount": 0
}
]
}

@Entity
@Table(name = "invoices")
public class Invoice implements Comparable<Invoice> {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "invoice_id")
private int id;
@Column(name = "date")
private LocalDate date = LocalDate.now();
@OneToOne(mappedBy = "invoice", cascade = {CascadeType.DETACH, CascadeType.MERGE,
CascadeType.PERSIST, CascadeType.REFRESH})
private Counterparty counterparty;

@OneToMany(mappedBy = "invoice", cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
private List<InvoiceItem> invoiceItems = new ArrayList<>();

@Entity
@Table(name = "items")
public class InvoiceItem {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id")
private int id;
private String description;
private int numberOfItems;
private BigDecimal amount;
private BigDecimal vatAmount;

@JoinColumn(name = "vat_code")
@Enumerated(EnumType.ORDINAL)
private Vat vat;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH})
@JoinColumn(name = "invoice_id")
private Invoice invoice;

我与 Counterparty 具有完全相同的映射,但它不显示 JSON 格式的发票数据:

@Id
private String nip;
private String companyName;
private String phoneNumber;
private String bankName;
private String bankNumber;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "counterparty", cascade = CascadeType.ALL)
private Address address;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH})
@JoinColumn(name = "invoice_id")
private Invoice invoice;

也许我的注释存在一些问题,发票被递归调用?

最佳答案

您应该使用 @JsonIgnore 注释标记 InvoiceItems 上的 Invoice 属性,这意味着当 InvoiceItems 序列化时,它不会被序列化为 JSON,这应该可以防止无限递归 JSON 问题:

 @Entity
@Table(name = "items")
public class InvoiceItem {

...

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH})
@JoinColumn(name = "invoice_id")
@JsonIgnore
private Invoice invoice;

关于java - JSON 在发布时递归调用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46091953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com