Could not write JSON: Infinite recursion (StackOverflowError); (已解决)

controller层采用@RestController注解,该注解会将结果用jackson序列化。

返回实体类

public class BmTreeNodeBean {
	private String id; // ID
	private String text; // 节点显示
	private String icon; // 图标
	private boolean leaf; // 是否叶子
	private String description; // 描述信息
	private List<BmTreeNodeBean> children;// 子节点信息
	private BmTreeNodeBean parentNode; // 父节点
	private Map<String, Object> columnValues;// 记录sql中查询的内容
	private BmConfTree confTree;// 本节点对应的数据
	private Integer nodeDepth;// 当前节点深度
	private Map<String, Object> nodeIdInfo;

	public BmTreeNodeBean() {
	}

	public BmTreeNodeBean(String id, String nodeText, boolean isLeaf, String desc, int nodeDepth) {
		this.setId(id);
		this.setText(nodeText);
		this.setLeaf(isLeaf);
		this.setDescription(desc);
		this.setNodeDepth(nodeDepth);
	}


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<BmTreeNodeBean> getChildren() {
		return children;
	}

	public void setChildren(List<BmTreeNodeBean> children) {
		this.children = children;
	}

	public BmTreeNodeBean getParentNode() {
		return parentNode;
	}

	public void setParentNode(BmTreeNodeBean parentNode) {
		this.parentNode = parentNode;
	}

	public Map<String, Object> getColumnValues() {
		return columnValues;
	}

	public void setColumnValues(Map<String, Object> columnValues) {
		this.columnValues = columnValues;
	}

	public BmConfTree getConfTree() {
		return confTree;
	}

	public void setConfTree(BmConfTree confTree) {
		this.confTree = confTree;
	}

	public Integer getNodeDepth() {
		return nodeDepth;
	}

	public void setNodeDepth(Integer nodeDepth) {
		this.nodeDepth = nodeDepth;
	}

	public Map<String, Object> getNodeIdInfo() {
		return nodeIdInfo;
	}

	public void setNodeIdInfo(Map<String, Object> nodeIdInfo) {
		this.nodeIdInfo = nodeIdInfo;
	}

}

执行会出现报错提示:

Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)

问题中出现主要是因为有双向“多对一”造成的

解决方法:

	@JsonManagedReference
	private List<BmTreeNodeBean> children;// 子节点信息
	@JsonBackReference
	private BmTreeNodeBean parentNode; // 父节点

加入这两个注解

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值