当前位置: 代码迷 >> 综合 >> Could not write JSON: Infinite recursion (StackOverflowError); (已解决)
  详细解决方案

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

热度:73   发布时间:2024-02-07 16:45:24.0

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

返回实体类

public class BmTreeNodeBean {private String id; // IDprivate 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)

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

解决方法:

	@JsonManagedReferenceprivate List<BmTreeNodeBean> children;// 子节点信息@JsonBackReferenceprivate BmTreeNodeBean parentNode; // 父节点

加入这两个注解

  相关解决方案