Override the JsonSerializer serialization method as shown below.
public class NullSerializer extends JsonSerializer<Object> { public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
And then create a custom mapper object and set the NullSearilizer to the default serializer, as shown below.
public class CustomJacksonObjectMapper extends ObjectMapper { public CustomJacksonObjectMapper() { super(); DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl(); sp.setNullValueSerializer(new NullSerializer()); this.setSerializerProvider(sp); } }
source share