Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonInclude(Include.NON_NULL) not working as expected

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 ?

like image 819
Ninad Pingale Avatar asked Oct 08 '14 12:10

Ninad Pingale


People also ask

What is @JsonInclude include Non_null?

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.

What is @JsonInclude annotation in spring boot?

@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.

What is @JsonInclude in java?

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.

What is @JsonInclude JsonInclude include Non_default?

If @JsonInclude(JsonInclude. Include. NON_DEFAULT) is used on the class level then default values of the fields are excluded.


1 Answers

I tried

@JsonSerialize(include = Inclusion.NON_NULL)

intead of

@JsonInclude(Include.NON_NULL)

and it worked as expected.

like image 96
Ninad Pingale Avatar answered Sep 18 '22 19:09

Ninad Pingale