Hide object reference in Jackson JSON serialization when using @JsonIdentityInfo

I have two objects: Category and Element.

Object Category:

@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 Element:

    @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 executing the left connection request: "SELECT c from category c Left JOIN FETCH c.itemList il"

I got the result, and then I serialized JSON to sleep mode to get the result.

My HibernateAwareObjectMapper:

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

And HibernateAwareSerializerFactory:

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

In my dispatcher servlet, I wrote:

    <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 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 the identifier of the object for the subject of the category.

And there is "@id": "75a3a7e5-ce66-4f6d-a04c-d04145d92b21" which is the identifier of the object for the object of the object.

3 . Catagory 2 . : "971ef69e-1605-46f2-8234-b595e38be11a" , "971ef69e-1605-46f2-8234-b595e38be11a"

JSON . "@id" json. "@id" @JsonIdentityInfo.

@JsonIdentityInfo, "@id" . @JsonIgnore, .

googled 2 , . - ?

+4

Source: https://habr.com/ru/post/1607602/


All Articles