我使用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;
...
}
我搜索了谷歌和SO,但没有运气。
反序列化包含 2 个具有相同 ID 的对象的 Json 需要哪些更改?
经过多次尝试,@JsonIgnoreProperties解决了我的问题。
示例:"@JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })"