I have added @JsonInclude(Include.NON_NULL)
annotation on Response class.
@JsonInclude(Include.NON_NULL)
public class Response {
@JsonProperty
private String message;
// getter-setters
}
If the value is null the property does not include in JSON
But still I am getting this property as a NULL.
{
"message": null
}
What can be the reason ? Am I missing anything ?
You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.
@JsonInclude(Include.NON_NULL) or @JsonInclude(JsonInclude.Include.NON_NULL) is used to ignore null fields in an object. In your particular example you have returned a String value that is why it is printing null.
The Jackson @JsonInclude annotation can be used to exclude the properties or fields of a class under certain conditions and it can be defined using the JsonInclude. Include enum.
If @JsonInclude(JsonInclude. Include. NON_DEFAULT) is used on the class level then default values of the fields are excluded.
I tried
@JsonSerialize(include = Inclusion.NON_NULL)
intead of
@JsonInclude(Include.NON_NULL)
and it worked as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With