0

私はjackson JsonIdentityInfoを使用して、spring mvcの再帰オブジェクト参照を処理しました。 同じIDを持つ2つのオブジェクトを含むJsonを逆シリアル化できないという問題が発生しました。jacksonを使用して同じIDを持つ2つのオブジェクトを含む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が、運を検索しました。

同じIDを持つ2つのオブジェクトを含むJsonを逆シリアル化するために必要な変更は何ですか?

+0

;' '民間セット<組織>組織へ;' – Patrick

+0

@Patrick私はList to Setを変更しました。まだ同じ問題。参考までに、私はマッピングテーブルを使用しています。だからデータ型の問題はありません。 – Krish

+0

@Krishnaあなたは単純な一般的な方法でそれを行うことはできないと思います。おそらくhttps://stackoverflow.com/a/24684251/1032167を試してみるかカスタムIDジェネレータを実装することができますが、カスタムシリアライザ/デシリアライザを読んで理解するのがずっと難しくなると思います。 – varren

答えて

0

多くの試行の後、@ JsonIgnorePropertiesが私の問題を解決しました。

例:あなたは `プライベートリスト<組織>組織を変更した場合に何が起こるか "@JsonIgnoreProperties(allowSetters =真、値= { "企業"})"

関連する問題