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

sucanber 发布于 2016/01/26 12:21
阅读 10K+
收藏 0

【深度】AI、5G时代下,算力网络与内生安全最全解析!>>>



对象是通过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)



请问如何解决这类问题?

加载中
0
彪大大
彪大大
主外键关联进入死循环了,建议tojson的时候注解一下 employee不进行序列化,具体在Employee实体中的employee属性上加@JSONField(serialize = false)注解,如果不行请自行搜索其他方法。你这个问题的更本是自关联进入了死循环。
返回顶部
顶部