2012-02-02 35 views
7
I have a scenario where i need to use the payload as 

{"authType":"PDS"} 
or 
{"authType":"xyz","authType2":"abc",} 
or 
{"authType":"xyz","authType2":"abc","authType3":"123"} 
or 
any combination except for null values. 

參考代碼我有3個字段,但只使用不是空值字段。 基本上我不想包括具有空值的字段。傑克遜:有條件地選擇字段

在那裏被用來完成它

public class AuthJSONRequest { 

    private String authType; 
    private String authType2; 
    private String authType3; 

    public String getAuthType() { 
     return authType; 
    } 

    public void setAuthType(String authType) { 
     this.authType = authType; 
    } 

    public String getAuthType2() { 
     return authType2; 
    } 

    public void setAuthType2(String authType2) { 
     this.authType2 = authType2; 
    } 
     public String getAuthType3() { 
     return authType3; 
    } 

    public void setAuthType3(String authType3) { 
     this.authType3 = authType3; 
    } 

} 

回答

5

這正是Jackson2中的註釋@JsonInclude和Jackson中的註釋@JsonSerialize的意思。

如果你想要一個屬性,只有當它不等於null,請添加@JsonInclude(Include.NON_NULL) resp。 @JsonSerialize(include=Include.NON_NULL)