Spring boot中关于多对多查询json无限递归问题

tech2024-01-10  96

控制台异常

#... java.lang.Illegal State Exception: Cannot call sendError() after the response has been committed #...

父类

BusinessTemplate.java

// ... @OneToMany(targetEntity = Link.class, mappedBy = "businessTemplate", fetch = FetchType.EAGER) private List<Link> links; // ...

子类

Link.java

//... @ManyToOne private BusinessTemplate businessTemplate; //...

问题描述

如果在LinkController中直接查询Link对象会出现json无限递归问题。

@JsonIdentityInfo解决

分别在父子类中添加如下注释: BusinessTemplate.java

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class BusinessTemplate { //... }

Link.java

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Link { //... }

json响应

{ "id": 4, "name": "环节名称", "businessTemplate": { "id": 2, "name": "预算业务", "type": "业务类型1", "links": [ 4 ] }, "nextLinkId": 0, "previousLinkId": 0 } { "id": 2, "name": "预算业务", "type": "业务类型1", "links": [ { "id": 4, "name": "环节名称", "businessTemplate": 2, "nextLinkId": 0, "previousLinkId": 0 } ] }

参考

Jackson – Bidirectional Relationships

最新回复(0)