杰克逊:条件选择领域

时间:2012-02-02 02:12:47

标签: jackson

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

}

2 个答案:

答案 0 :(得分:10)

尝试JSON视图?请参阅thisthis。或者,有关更多过滤功能,请参阅this blog entry(例如Json过滤器)。

答案 1 :(得分:5)

这正是杰克逊2中的注释@JsonInclude和杰克逊中@JsonSerialize的注释。

如果您希望某个属性仅在不等于null时显示,请添加@JsonInclude(Include.NON_NULL) resp。 @JsonSerialize(include=Include.NON_NULL)

相关问题