杰克逊与双向一对多关系混淆 [英] Jackson confused with bidirectional one-to-many relationship

查看:80
本文介绍了杰克逊与双向一对多关系混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过MappingJacksonHttpMessageConverter使用带有Hibernate / Spring MVC的jackson 1.9.2。

I'm using jackson 1.9.2 with Hibernate/Spring MVC through MappingJacksonHttpMessageConverter.

杰克逊无法序列化双向一对多关系并进行无限循环。

Jackson can not serialize bidirectional one-to-many relationship and makes an infinite loop.

类I使用的是:


  • 包含一组SMS实例的会话。

  • Conversation which has a Set of SMS instances.

每个SMS实例都有一组PhoneNumbers

Each SMS instance has a Set of PhoneNumbers

每个PhoneNumber都有一个父联系人(这是双向的多对一关系)

Each PhoneNumber has a parent contact (this is the bidirectional many-to-one relationship)

我要做的是序列化对话。

What I am trying to do is to serialize a conversation.

如果我不使用 @JsonManagedReference @JsonBackReference ,那么jackson会因无限循环而崩溃。但是当我使用它们时, 联系 不会被序列化为 PhoneNumber

If I don't use @JsonManagedReference and @JsonBackReference then jackson will crashe due to an infinite loop. But when I use them, the Contact doesn't get serialized into the PhoneNumber.


Class Contact {
  @JsonManagedReference
  List<PhoneNumber> phoneNumber ;
}
Class PhoneNumber {
  @JsonBackReference 
  Contact contact;
}

输出为:


{    <--------------------- Conversation
    "id": 51,
    "smsSet": [
      {
        "id": 53,
        "origin": 0123465,
        "destination": "06533844XY",
        "message": "Hello world!",
        "phoneNumbers": [
          {
            "id": 64,
            "num": "06533844XY",
            "creationDate": 1333992533000,
          }
        ],
      }
    ],
    "creationDate": 1333992534000
  }

而不是


{    <---------- conversation
    "id": 51,
    "smsSet": [
      {
        "id": 53,
        "origin": 0123465,
        "destination": "06533844XY",
        "message": "Hello world!",
        "phoneNumbers": [
          {
            "id": 64,
            "num": "06533844XY",
            "creationDate": 1333992533000,
            "contact":  <--------------------- Missing part
             {
                "id": 12,
                "name": "Samuel Jackson",
                "primaryNumber": "06533844XY"
             }
          }
        ],
      }
    ],
    "creationDate": 1333992534000
  }


推荐答案

我最近遇到了类似的问题:Jackson - 具有双向关系的实体的序列化(避免循环)

I recently encountered a similar problem: Jackson - serialization of entities with birectional relationships (avoiding cycles)

因此解决方案是升级到Jackson 2.0,并添加到以下类中注释:

So the solution is to upgrade to Jackson 2.0, and add to classes the following annotation:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, 
                  property = "@id")
public class SomeEntityClass ...

然后问题是Spring无法与Jackson 2.0一起使用。这已通过以下方式解决:

Then the problem is that Spring doesn't work with Jackson 2.0. This has been solved in the following way:

<bean id="jacksonMessageConverter"
          class="own.implementation.of.MappingJacksonHttpMessageConverter"/>

<bean class="org.springframework.web.servlet.mvc
             .annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
        <property name="requireSession" value="false"/>
    </bean>

并且own.implementation.of.MappingJacksonHttpMessageConverter基于此:

And the own.implementation.of.MappingJacksonHttpMessageConverter is based on this:

http://www.jarvana.com/jarvana/view/org/springframework/spring- web / 3.0.0.RELEASE / spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java?format = ok

但是使用 ObjectMapper 和Jackson 2.0中的其他杰克逊课程而不是杰克逊1。*

But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.*

这篇关于杰克逊与双向一对多关系混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆