2016-04-19 2 views
0

私はこのような主な目的があります:私は「基本」タイプなどの子オブジェクトの唯一のプロパティでこれをシリアル化したい直列化オブジェクトインスタンスの基本的なプロパティジャクソン

public class MainObject{ 

     @Column(unique = false, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0) 
     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     private Long id; 

     @Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0) 
     @Basic 
     private String name; 

     @JsonIgnoreProperties({"address" ... }) 
     @ManyToOne(optional = true, targetEntity = Company.class) 
     private Child child; 

    } 

を。私の最善の解決策は、子オブジェクトの他のすべてのプロパティを無視することです。

私の目標は、このような何かを得るです:

{ 
    "id" = 12, 
    "name" = "some name", 
    "company" = 42 
} 

をこれまでのところ、私の最善の解決策、魔女と私は、次の取得アッパー見ることができます:

{ 
     "id" = 12, 
     "name" = "some name", 
     "company" = { 
      "id" = 42 
     } 
    } 

私が知っているが注釈魔女であります私は子供のオブジェクトで使用することができますが、ここでは難しい部分が来て、私のプログラムの他の部分では、私は子供を正常にシリアル化したい。

public class Child{ 

    @Column(unique = false, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0) 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0) 
    @Basic 
    private String name; 

    @Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0) 
    @Basic 
    private String address; 

    ... 
} 

私は欲しいものを得るための簡単な方法がありますか?

+1

そのシンプルでありながらなんとかではないhttpで簡単な方法を作成します// www.baeldung.com/jackson-custom-serialization – gigadot

答えて

0

助けてくれてありがとう@gigadot、しかし、私はレイジーな方法を見つけました。

だから私はただ単に子供

@JsonIgnore 
@ManyToOne(optional = true, targetEntity = Company.class) 
private Child child; 

無視し、私は@JsonProperty( "子")

@JsonProperty("child") 
    public Long getChildId(){ 
     return this.child.getId(); 
    }