我已经使用jackson JsonIdentityInfo处理spring mvc中的递归对象引用。我遇到了一个问题,即无法反序列化包含2个具有相同ID的对象的Json。
{ "organizations": [ { "organizationId": 1, "organizationName": "org1", "enterprise": { "enterpriseId": 1, "enterpriseName": "ent1", "organizations": null } }, { "organizationId": 2, "organizationName": "org2", "enterprise": 1 } ] }
如果您在上面看到,则两个组织都映射有企业“ 1”。对于第一个组织,它是整个企业对象,但对于组织2,它仅给出ID。我还需要获取组织2的整个对象。
我的POJO声明:
@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; ... }
我搜索了谷歌,所以没有运气。
反序列化包含2个具有相同ID的对象的Json需要进行哪些更改?
经过大量尝试,@ JsonIgnoreProperties解决了我的问题。
示例:“ @ JsonIgnoreProperties(allowSetters = true,值= {“企业”})”