使用@JsonIdentityInfo时Jackson在JSON序列化中隐藏对象引用

使用@JsonIdentityInfo时Jackson在JSON序列化中隐藏对象引用,json,hibernate,jackson,jsonidentityinfo,Json,Hibernate,Jackson,Jsonidentityinfo,我有两个实体:类别和项目 类别实体: @JsonInclude(Include.NON_EMPTY) @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class) @Entity public class Category { @Id @GeneratedValue private int id; private String categoryName; @OneTo

我有两个实体:类别和项目

类别实体:

@JsonInclude(Include.NON_EMPTY)
@JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
@Entity
public class Category {
    @Id
    @GeneratedValue
    private int id;

    private String categoryName;    

    @OneToMany(mappedBy = "category")   
    private List<Item> itemList;

    //have getters and setters  
}
执行如下左连接查询后: “从类别c左连接获取c.itemList il中选择c”

我得到了结果,然后对结果进行了支持hibernate的JSON序列化

我的HibernateAwareObject映射器是:

public class HibernateAwareObjectMapper extends ObjectMapper {
          public HibernateAwareObjectMapper() {   
                Hibernate4Module hibernateModule = new Hibernate4Module();
                hibernateModule.disable(Hibernate4Module.Feature.FORCE_LAZY_LOADING);
                registerModule(hibernateModule);
          }
        } 
HibernateAwareSerializerFactory是:

public class HibernateAwareSerializerFactory extends BeanSerializerFactory {
                protected HibernateAwareSerializerFactory(SerializerFactoryConfig config) {
                    super(config);
                }
            }
在我的dispatcher servlet中,我编写了:

    <mvc:annotation-driven>
                <mvc:message-converters>
                    <bean
                        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="objectMapper">
                            <bean class="org.scinv.hibernateAwareMapper.HibernateAwareObjectMapper" />
                        </property>
                    </bean>
                </mvc:message-converters>
        </mvc:annotation-driven>
在这个JSON中, 有“@id”:“971ef69e-1605-46f2-8234-b595e38be11a” 这是类别实体的对象Id

还有“@id”:“75a3a7e5-ce66-4f6d-a04c-d04145d92b21” 这是项实体的对象Id

类别有3个项目,因此类别的对象Id也显示了2次以上。 像这样:“971ef69e-1605-46f2-8234-b595e38be11a”,“971ef69e-1605-46f2-8234-b595e38be11a”

生成的JSON序列化正如预期的那样。 但我只需要在json中隐藏对象引用“@id”。默认情况下,此“@id”由@JsonIdentityInfo创建

所以我需要配置@JsonIdentityInfo来隐藏对象Id“@Id”。 我尝试过@JsonIgnore,但失败了


我在谷歌上搜索了2天的解决方案,但失败了。有人能给我一个解决方案或建议吗

您是如何实现“支持hibernate的JSON序列化”的?我认为问题是there@MangEngkus我添加了HibernateAwareSerializerFactory、HibernateAwareObjectMapper和DispatcherServlet。但我认为它们是可以的。因为它正在按预期生成JSON序列化。但我只需要在该JSON中隐藏对象引用//@id//。默认情况下,它是由//@JsonIdentityInfo//创建的。因此,我需要对//@JsonIdentityInfo//进行配置,以隐藏对象引用“//@id//”如果不想让注释生成对象引用,为什么要使用注释?如果无法控制修改源代码,则可以在
HibernateAwareObjectMapper
上禁用注释是否尝试
@JsonManageReference
@JsonBackReference
以避免无限递归?是,我使用了
@JsonManageReference
@JsonBackReference
。但这次我想使用
@JsonIdentityInfo
。此
@JsonIdentityInfo
自动生成如下对象Id:
“@Id:“75a3a7e5-ce66-4f6d-a04c-d04145d92b21”
我只想在JSON中隐藏此对象Id。
    <mvc:annotation-driven>
                <mvc:message-converters>
                    <bean
                        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                        <property name="objectMapper">
                            <bean class="org.scinv.hibernateAwareMapper.HibernateAwareObjectMapper" />
                        </property>
                    </bean>
                </mvc:message-converters>
        </mvc:annotation-driven>
   {
      "ArrayList": [
        {
          "@id": "971ef69e-1605-46f2-8234-b595e38be11a",
          "id": 1,
          "categoryName": "fruit",
          "itemList": [
            {
              "@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21",
              "id": 1,
              "itemName": "mango",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            },
            {
              "@id": "0aa0fb71-2909-4909-8403-0765829ee8c1",
              "id": 2,
              "itemName": "apple",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            },
            {
              "@id": "02c381cb-33fa-45a6-bff9-ec146357f4bc",
              "id": 3,
              "itemName": "orange",
              "category": "971ef69e-1605-46f2-8234-b595e38be11a"
            }
          ]
        },
        "971ef69e-1605-46f2-8234-b595e38be11a",
        "971ef69e-1605-46f2-8234-b595e38be11a"    
      ]
    }