本文介绍了杰克逊自我参考导致循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用 Jackson 将休眠对象转换为 JSON 时遇到问题,因为某些对象在其定义中具有自引用.问题是我不控制这些实体的代码,所以我不能在那里放注释.

实际上我只想限制递归的深度,例如限制为 5 个级别.我需要适用于任何包含自引用的实体对象的通用代码.这可能吗?我不介意使用另一个 JSON 库.

以下简单代码

ObjectMapper mapper = new ObjectMapper();Query q = session.createQuery("来自危险");列出危害 = q.list();对于(危害 h:危害){字符串危险Json;hazardJson = mapper.writeValueAsString(h);}

给我一​​个例外:

org.codehaus.jackson.map.JsonMappingException:直接自引用导致循环(通过引用链:com.fgm.imsma.pojo.Hazard["location"]->com.fgm.imsma.pojo.Location["location"])在 org.codehaus.jackson.map.ser.BeanPropertyWriter._reportSelfReference(BeanPropertyWriter.java:473)在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:411)在 org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:428)在 org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)在 org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:587)在 org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:245)在 org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:1993)在 org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:1595)在 imsma.json.GetObjects.main(GetObjects.java:47)

预先感谢您的任何建议!

解决方案

使用 Jackson 2.0 - 它处理循环引用(使用 @JsonIdentityInfo 注释)

如果您不能直接向类添加注释,则使用 MixIn 注释.这里的一个例子:https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations


I have a problem when converting hibernate objects to JSON with Jackson, because some objects have a self-reference in their definition.The problem is that I don't control the code of those entities so I cannot put annotations in there.

Actually I would like to just limit the depth of the recursivity, for example to 5 levels. I need generic code that works with any entity object that contains self reference.. Is it possible?I don't mind use another JSON library.

The following simple code

ObjectMapper mapper = new ObjectMapper();

Query q = session.createQuery("from Hazard ");
List<Hazard> hazards = q.list();

for (Hazard h : hazards) {
    String hazardJson;
    hazardJson = mapper.writeValueAsString(h);
}

Gives me exception:

org.codehaus.jackson.map.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.fgm.imsma.pojo.Hazard["location"]->com.fgm.imsma.pojo.Location["location"])
    at org.codehaus.jackson.map.ser.BeanPropertyWriter._reportSelfReference(BeanPropertyWriter.java:473)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:411)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:428)
    at org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:587)
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:245)
    at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:1993)
    at org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:1595)
    at imsma.json.GetObjects.main(GetObjects.java:47)

Thanks in advance for any suggestions!

解决方案

Use Jackson 2.0 - it handles cyclic references (with @JsonIdentityInfo annotation)

If you cannot add annotations directly to the class then use MixIn annotations. An example here: https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations

这篇关于杰克逊自我参考导致循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:57