使用@JsonIdentityInfo会产生一个不相关的ID

时间:2018-10-23 15:11:17

标签: hibernate rest spring-boot jackson

为了防止在实体的Jackson序列化期间进行无限递归,我使用了@JsonIdentityInfo批注。

此实体的特殊性是包含对自身类型相同的父实体和子实体的引用。

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id")
@Entity
@Table(name = "place")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Place extends Thing implements Serializable {

    ...

    @OneToMany(mappedBy = "containedInPlace", fetch = FetchType.EAGER)
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Place> containsPlaces = new HashSet<>();

    @ManyToOne
    private Place containedInPlace;
}

结果是一个JSON,其中包含所有元素,但其中某些元素会转换为唯一ID,从而丢失所有信息。

[  
   {  
      "id":1003,
      "name": "AAA",
      "containsPlaces":[  

      ],
      "containedInPlace":{  
         "id":1001,
         "name": "BBB",
         "containsPlaces":[  
            1,
            {  
               "id":1004,
               "name": "CCC",
               "containsPlaces":[  

               ],
               "containedInPlace":2
            },
            {  
               "id":1005,
               "name": "DDD",
               "containsPlaces":[  

               ],
               "containedInPlace":2
            },
            {  
               "id":1006,
               "name": "EEE",
               "containsPlaces":[  

               ],
               "containedInPlace":2
            },
            {  
               "id":1007,
               "name": "FFF",
               "containsPlaces":[  

               ],
               "containedInPlace":2
            }
         ],
         "containedInPlace":null,
      }
   },
   3,
   4,
   5,
   6
]

有没有一种方法来获取完整的对象而不是id? 还有其他方法可以获得一个实体的父子级的完整序列化而不会陷入无限循环吗?

0 个答案:

没有答案