Hide Object reference in JSON serialization by Jackson when using @JsonIdentityInfo

766 Views Asked by At

I have two entities: Category and Item.

Category Entity:

@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  
}

Item Entity:

    @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class)
    @Entity
    public class Item {

    @Id
    @GeneratedValue
    private int id;
    private String itemName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_Category_id")    
    private Category category;

    //have getters and setters              
}

After doing a left join query like this: "SELECT c from Category c Left JOIN FETCH c.itemList il"

I got the result and then I have done hibernate aware JSON serialization to the result.

My HibernateAwareObjectMapper is:

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

And HibernateAwareSerializerFactory is:

public class HibernateAwareSerializerFactory extends BeanSerializerFactory {
                protected HibernateAwareSerializerFactory(SerializerFactoryConfig config) {
                    super(config);
                }
            }

In my dispatcher servlet I have written:

    <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>

So, I have got a JSON Like this:

   {
      "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"    
      ]
    }

In this JSON, there is "@id": "971ef69e-1605-46f2-8234-b595e38be11a" which is Object Id for Category Entity.

And there is "@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21" which is Object Id for Item Entity.

Category has 3 items So Catagory's object Id is also showing 2 more times. Like this: "971ef69e-1605-46f2-8234-b595e38be11a","971ef69e-1605-46f2-8234-b595e38be11a"

That Generated JSON serialization is as expected. But I just need to hide that Object reference "@id" in that json. This "@id" is by default created by @JsonIdentityInfo.

So I need configuration for @JsonIdentityInfo to hide Object Id "@id". I have tried @JsonIgnore but failed.

I have googled 2 days for solution but failed.Can anyone give me a solution or a suggestion?

0

There are 0 best solutions below