Override @JsonInclude(Include.NON_NULL) from POJO in ObjectMapper

Viewed 1883

Question: Is it possible to override Include.NON_NULL defined in the POJO while creating the ObjectMapper?

Explanation:

Suppose I have a POJO as below:

@JsonInclude(Include.NON_NULL)
class POJO {
  String name;
  String description;
  //Constructors, Getters & Setters, etc
}

And a test class as below:

class Main {
  public static void main(String args[]) {
    POJO p = new POJO();
    ObjectMapper mapper = new ObjectMapper();
     String jsonString = mapper.setSerializationInclusion(Include.ALWAYS)
                               .writerWithDefaultPrettyPrinter()
                               .writeValueAsString(p);
    //jsonString should contain empty name & description fields, but they doesn't
  }
}
1 Answers
Related