Web Services @JsonInclude(Include.NON_NULL)未按预期方式工作

shstlldc  于 2022-11-15  发布在  其他
关注(0)|答案(5)|浏览(140)

我在Response类上添加了@JsonInclude(Include.NON_NULL)注解。

@JsonInclude(Include.NON_NULL)
public class Response {

  @JsonProperty
  private String message;

 // getter-setters
}

如果值为null,则属性不包含在JSON中
但我仍然将此属性作为NULL获取。

{
"message": null
}

是什么原因?我错过了什么吗?

3df52oht

3df52oht1#

我试过了

@JsonSerialize(include = Inclusion.NON_NULL)

代替

@JsonInclude(Include.NON_NULL)

而且效果和预期的一样。

arknldoa

arknldoa2#

这是正确的使用方法。这是我从jdk8+有效。
@JsonInclude(JsonInclude.Include.NON_EMPTY)
有关详细信息,请访问:json-include-non-empty

xpcnnkqh

xpcnnkqh3#

我正在使用Java Spring &在其中,我正在实现如下内容:

班级级别:

import com.fasterxml.jackson.annotation.JsonInclude;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        public class Student {

            String name;
            //getter-setter
    }

您也可以在类的元素级别上实现它:

元素级别:

import com.fasterxml.jackson.annotation.JsonInclude;

        @JsonInclude(JsonInclude.Include.NON_NULL)
        public class Student {

            String name;

            @JsonInclude(JsonInclude.Include.NON_NULL)
            String address;
            //getter-setter
    }
1tu0hz3e

1tu0hz3e4#

我遇到了同样的问题。

@JsonInclude(value = Include.NON_EMPTY, content = Include.NON_NULL)
vof42yt1

vof42yt15#

不建议使用:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

请用途:

@JsonInclude(Include.NON_NULL)

相关问题