0

By using @JsonInclude(JsonInclude.Include.NON_EMPTY) at class level it ignores null and empty value tags during serialization.

Is there any variant of JsonInclude or any other library or class where we can ignore null and empty properties of associated (has-a) class.

  • have you tried putting the @JsonInclude on the associated class – Ala Abid Oct 27 '20 at 09:05
  • it works if I put @JsonInclude on associated class. But I need to put this annotation on every associated class of mine so was thinking if any global annotation which recursively check for null values on associated class then that could be great. –  Oct 27 '20 at 09:21
  • well then, i think the links provided in the first answer would do the job :) – Ala Abid Oct 27 '20 at 10:21

1 Answers1

0

You have the option to do with annotation at the class level, i.e:

@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case 
public static class Request {
  // ...
}

As noted in comments, in versions below 2.x the syntax for this annotation is:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY

The other option is to configure the ObjectMapper directly, simply by calling mapper.setSerializationInclusion(Include.NON_NULL);

As mentioned by drew moore You maybe also can take a look on Ignore Null Fields with Jackson - Baeldung