2017-12-14 14 views
1

私は、(私が受け取った)jsonドキュメントをJavaオブジェクトにマップするPOJOクラスを持っています。同じPOJOクラスのさまざまな出力のカスタムアノテーションを作成します

@JsonInclude(JsonInclude.Include.NON_EMPTY) 
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE) 
public class Product implements Serializable, UniqueKeyAware { 

    private static final long serialVersionUID = -7311148654827944888L; 

    @JsonIgnore 
    private String uniqueKey; 

    @JsonProperty("uniqueKey") 
    private String uniqueKeyV2; 

    @JsonProperty("gtin") 
    private String gtin; 

    @JsonProperty("printedGtin") 
    private String printedGtin; 

    @JsonProperty("tpnb") 
    private String tpnb; 

    @JsonProperty("tpnc") 
    private String tpnc; 

    @JsonProperty("tpna") 
    private String tpna; 

    @JsonProperty("itemNumber") 
    private String itemNumber; 

    @JsonProperty("catId") 
    private String catId; 

    @JsonProperty("styleCode") 
    private String styleCode; 

    @JsonProperty("description") 
    private String description; 

    @JsonProperty("brand") 
    private String brand; 

    @JsonProperty("isOwnLabel") 
    private Boolean isOwnLabel; 

    @JsonProperty("regulatedProductName") 
    private String regulatedProductName; 

    @JsonProperty("country") 
    private List<Country> region; 

    ... // remove for simplicity 

私が望むのは、jsonから4つの別々のドキュメントを作成する最良の方法です。

したがって、このpojoクラスのサブセットである4つのクラスを定義できます。

  • 公開
  • プライベート
  • パートナー
  • Priviledge

コンフィギュラの目的のために、私は@public、@private、@partner、私は上記の書き込みを行います@priviledgeのようなカスタム注釈を必要としますすべてのフィールド。実行時に、例えば、 @publicアノテーションが記述されているフィールドだけのインスタンスが作成されます。

これを実装する必要があります。私はこれがジャックソンのライブラリにいくつかのフックを作成することによって可能だと思います。私は一日でそれをする必要があります、誰もちょうどこれを行う方法を指導することができます。

@JsonInclude(JsonInclude.Include.NON_EMPTY) 
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE) 
public class Product implements Serializable, UniqueKeyAware { 

    private static final long serialVersionUID = -7311148654827944888L; 

    @JsonIgnore 
    private String uniqueKey; 

    @JsonProperty("uniqueKey") @public @private 
    private String uniqueKeyV2; 

    @JsonProperty("gtin") @public 
    private String gtin; 

    @JsonProperty("printedGtin") @public 
    private String printedGtin; 

    @JsonProperty("tpnb")@private 
    private String tpnb; 

    @JsonProperty("tpnc")@private 
    private String tpnc; 

    @JsonProperty("tpna")@priviledge 
    private String tpna; 

    ... // removed for simplicity 

上記例えば中:

最終生成物は、これを好むべきです@ publicインスタンスは、属性としてuniqueKey、gtin、およびprintGtinを持ちます。

答えて

1

解決策が見つかりました。私たちが必要とするのは、すでにジャクソンに存在する@JsonViewアノテーションです。まず、次のようなビュークラスを作成する必要があります。

public class Views 
{ 
    public static class Public{}; 
    public static class Partner extends Public{}; 
    public static class Priviledge extends Partner {}; 
    public static class Private extends Priviledge {}; 
} 

次に、主な製品クラスは次のようになります。

ObjectMapper mapper = new ObjectMapper(); 
MyProduct product = mapper.readerWithView(Views.Public.class).forType(MyProduct.class).readValue(json) 

@JsonInclude(JsonInclude.Include.NON_EMPTY) 
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE) 
public class MyProduct implements Serializable, UniqueKeyAware { 

    private static final long serialVersionUID = -7311148654827944888L; 

    @JsonIgnore 
    private String uniqueKey; 

    @JsonProperty("uniqueKey") 
    @JsonView(Views.Partner.class) 
    private String uniqueKeyV2; 

    @JsonProperty("gtin") 
    @JsonView(Views.Public.class) 
    private String gtin; 

    @JsonProperty("printedGtin") 
    @JsonView(Views.Public.class) 
    private String printedGtin; 

    @JsonProperty("tpnb") 
    @JsonView(Views.Public.class) 
    private String tpnb; 

    // skipped other attributes and getter setter 

主な機能は次のようになります

関連する問題