9

I have a simple POJO class that extends another simple POJO class. I am using the com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy to marshall the properties in these POJO classes to JSON. However, when I set some of the properties to the POJO as null, then it outputs those properties as the string null instead of not outputting it at all.

for eg.

{
   Person:
   [{
      "firstName":"John"
      "lastName":"null"
    }]
}

instead of:

for eg.

{
   Person:
   [{
      "firstName":"John"
    }]
}
Kirby
  • 15,127
  • 10
  • 89
  • 104
Salil Surendran
  • 2,245
  • 4
  • 27
  • 42

1 Answers1

18

Different options are available for suppressing serialization of properties with null values, depending on the version of Jackson in use, and whether the ObjectMapper can be directly configured.

With Jackson 1.1+, with direct access to configure the ObjectMapper, you could just call setSerializationInclusion(Include.NON_NULL).

Alternatively, you could annotate the (class) type that has the properties, for which null properties serialization is to be suppressed, with @JsonSerialize(include=Inclusion.NON_NULL).

With Jackson 2+, instead of the @JsonSerialize annotation, use @JsonInclude(Include.NON_NULL).

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97