Unable to deserialize Json that contain 2 objects with the same ID using jackson

北城余情 提交于 2019-12-02 18:06:38

问题


I have used jackson JsonIdentityInfo to handle the recursive object reference in spring mvc. I came across one issue i.e., Unable to deserialize Json that contain 2 objects with the same ID.

{

    "organizations": [
        {
            "organizationId": 1,            
            "organizationName": "org1",
            "enterprise": {
                "enterpriseId": 1,              
                "enterpriseName": "ent1",
                "organizations": null
            }
        },
        {
            "organizationId": 2,            
            "organizationName": "org2",
            "enterprise": 1
        }
    ]
}

if you see above, both organizations are mapped with enterprise "1". For the first organization it is whole enterprise object but for organization 2, it is giving ID only. I need to get the whole object for organization 2 as well.

My POJO declarations:

@Entity
@Table(name = "organization")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "organizationId")
public class Organization implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "organization_id")
    private Long organizationId;

    ...

    @ManyToOne
    @JoinTable(name = "enterprise_organization", joinColumns = {
            @JoinColumn(name = "organization_id") }, inverseJoinColumns = { @JoinColumn(name = "enterprise_id") })
    private Enterprise enterprise;

    ...
}

@Entity
@Table(name = "enterprise")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "enterpriseId")
public class Enterprise extends BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "enterprise_id")
    private Long enterpriseId;
    ...

    @OneToMany(mappedBy = "enterprise")
    private List<Organization> organizations;
    ...
}

I searched google and SO but no luck.

What are the changes needed to deserialize Json that contain 2 objects with the same ID ?


回答1:


After a lot of tries, @JsonIgnoreProperties solved my problem.

example: "@JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })"



来源:https://stackoverflow.com/questions/47078057/unable-to-deserialize-json-that-contain-2-objects-with-the-same-id-using-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!