2012-02-02 13 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. 

odnosząc się do kodu Mam 3 pola, ale tylko pola o wartości pustej nie są używane. Zasadniczo nie chcę uwzględniać pola, które ma wartość pustą.Jackson: Warunkowo wybierz pola

Czy istnieją jakieś adnotacje mają być stosowane, aby zrobić to

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; 
    } 

} 

Odpowiedz

5

Jest to dokładnie to, co oznaczono jako @JsonInclude w Jackson2 i @JsonSerialize w Jackson.

Jeśli chcesz, aby właściwość pojawiała się tylko wtedy, gdy nie jest równa null, dodaj @JsonInclude(Include.NON_NULL) lub @JsonSerialize(include=Include.NON_NULL).

Powiązane problemy