开发者社区> 问答> 正文

fastjson 对象转json string 解决循环引用报错(溢出)?报错



对象是通过hibernate 查询的一对一双向关系的对象:

解析代码:

Employee e = employeeService.findOne(2);
        System.out.println(e);

        String str = JSON.toJSONString(e, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(str);



报错:

java.lang.StackOverflowError
	at com.alibaba.fastjson.serializer.SerializerFeature.isEnabled(SerializerFeature.java:148)
	at com.alibaba.fastjson.serializer.SerializeWriter.isEnabled(SerializeWriter.java:130)
	at com.alibaba.fastjson.serializer.JSONSerializer.isEnabled(JSONSerializer.java:335)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.isWriteAsArray(JavaBeanSerializer.java:293)
	at Serializer_2.write1(Unknown Source)
	at Serializer_2.write(Unknown Source)
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:403)
	at Serializer_1.write1(Unknown Source)
	at Serializer_1.write(Unknown Source)
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:403)
	at Serializer_2.write1(Unknown Source)
	at Serializer_2.write(Unknown Source)
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:403)
	at Serializer_1.write1(Unknown Source)
	at Serializer_1.write(Unknown Source)
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:403)
	at Serializer_2.write1(Unknown Source)
	at Serializer_2.write(Unknown Source)
	at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:403)
	at Serializer_1.write1(Unknown Source)



请问如何解决这类问题?

展开
收起
爱吃鱼的程序员 2020-06-10 14:37:15 2292 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    主外键关联进入死循环了,建议tojson的时候注解一下employee不进行序列化,具体在Employee实体中的<spanstyle="font-size:13.3333px;">employee属性上加<spanstyle="font-family:微软雅黑,Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">@JSONField(serialize=false)注解,如果不行请自行搜索其他方法。你这个问题的更本是自关联进入了死循环。

    2020-06-10 14:37:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多