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

控制台异常

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

父类

BusinessTemplate.javajava

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

子类

Link.javajson

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

问题描述

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

@JsonIdentityInfo解决

分别在父子类中添加以下注释: BusinessTemplate.javaapp

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

Link.javafetch

@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 Relationshipscode

相关文章
相关标签/搜索