杰克逊:条件选择领域

Rah*_*ddy 7 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.
Run Code Online (Sandbox Code Playgroud)

参考代码我有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;
    }

}
Run Code Online (Sandbox Code Playgroud)

Sta*_*Man 10

试试JSON视图?看到这个这个.或者有关更多过滤功能,请参阅此博客条目(例如Json Filters).


blu*_*der 5

这正是杰克逊@JsonInclude2和@JsonSerialize杰克逊中的注释所针对的.

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