2016-07-16 12 views
0

私はそれを宣言したクラスからいくつかのメンバ変数を無視する方法を知っているかもしれません。たとえば、PersonalInfoであり、AcedemicInfoFamilyInfoで宣言されている3つのクラスを次に示します。JSONにシリアル化するときに、宣言されたオブジェクトの変数の一部を無視しますか?

public class PersonalInfo { 
    @JsonPropetry 
    private String name; 

    @JsonPropetry 
    String universityName; 

    @JsonPropetry 
    private String motherName; 

    @JsonPropetry 
    private String fatherName; 

    /* Set and Get*/ 
} 

public class AcademicInfo { 
    @JsonPropetry 
    private PersonalInfo info; // need name and university only 

    /* Set and Get*/ 
} 

public class FamilyInfo { 
    @JsonPropetry 
    private PersonalInfo info; // need name and fatherName and motherName only 

    /* Set and Get*/ 
} 

はしかし、私はPersonalInfoからすべての属性を必要としないAcedemicInfoFamilyInfoなどPersonalInfoのメンバ変数のいくつかを無視する必要があります。

以下

は私の所望の出力

// Acedemic info json 
{ 
    "info" : { 
     "name":"Adam", 
     "universityName":"University" 
     } 
} 

// Family info json 
{ 
    "info" : { 
     "name":"Adam", 
     "fatherName":"Matt" 
     "motherName":"Jane" 
     } 
} 

である私は約@JsonIgnoreを知っているが、私はPersonalInfoクラスに注釈を入れた場合、変数がどのような私ではないことを宣言するすべてのクラスによって無視されます欲しいです。変数を条件付きで無視する方法を知っていますか?私の悪い英語を申し訳ありません。

+0

@JsonIgnoreを試しましたか? – Rustam

+0

@JsonIgnoreはあなたのためにこれを解決するはずです – user3765370

+0

@Rustam、はい、私はそれを試みました。しかし私は私の場合にそれをどのように適用するのか分かりません。条件付きで '@ JsonIgnore'を適用できますか、それを呼び出すクラスに依存できますか? – karfai

答えて

0

@JsonFilterSimpleFilterProviderSimpleBeanPropertyFilterを使用すると、シリアル化しないプロパティを除外できます。以下のようになります。

あなたのクラスは次のとおりです。

SimpleFilterProvider filterProvider = new SimpleFilterProvider(); 
filterProvider.addFilter("academicPersonalInfoFilter", 
     SimpleBeanPropertyFilter.serializeAllExcept("motherName", "fatherName")); 

ObjectMapper mapper = new ObjectMapper(); 
mapper.setFilters(filterProvider); 
mapper.writeValueAsString(academicInfo); 

行う別の方法は、シリアル化するプロパティのセットを定義するために使用し、複数の@JsonView次のとおりです。

public class AcademicInfo { 
    @JsonFilter("academicPersonalInfoFilter") 
    private PersonalInfo info; 
} 

との例では、オブジェクトをシリアル化します。

public class Views { 

    public static class BasicPersonalInfo { 
    } 

    public static class AcademicPersonalInfo extends BasicPersonalInfo { 
    } 

    public static class FamilyPersonalInfo extends BasicPersonalInfo { 
    } 
} 

をフィールドに注釈を付ける対応するビューにシリアル化する:

あなた場合は、次のようなビューを定義することができ

public class PersonalInfo { 
    @JsonView(Views.BasicPersonalInfo.class) 
    private String name; 

    @JsonView(Views.AcademicPersonalInfo.class) 
    String universityName; 

    @JsonView(Views.FamilyPersonalInfo.class) 
    private String motherName; 

    @JsonView(Views.FamilyPersonalInfo.class) 
    private String fatherName; 
} 

を、次のようにオブジェクトをシリアル化:

String result = mapper.writerWithView(Views.AcademicPersonalInfo.class) 
         .writeValueAsString(academicInfo); 
+0

私の質問にお答えいただきありがとうございます。しかし、私は自分の 'object'を' ResponseEntity.ok()。body(object) 'に渡します。 '@JsonFilter'や' @JsonView'がどこに適用できるのか分かりますか?あなたはそれについて考えていますか? – karfai

+0

'@ JsonView'についてhttps://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring – Wilson

+0

あなたのリンクをありがとう、今私は数字ですどのように春にそれを行うには。また、私の質問では春を言及しなかったことを残念に思っています。 '@ JsonView'はシリアライゼーションとデシリアライゼーションをサポートしているので、クラスを少なくする時間を節約できます。 – karfai

関連する問題