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; // 父节点
加入这两个注解