Override the JsonSerializer serialize method as below.
public class NullSerializer extends JsonSerializer<Object> {
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
// any JSON value you want...
jgen.writeString("");
}
}
then you can set NullSerializer
as default for custom object mapper:
public class CustomJacksonObjectMapper extends ObjectMapper {
public CustomJacksonObjectMapper() {
super();
DefaultSerializerProvider.Impl sp = new DefaultSerializerProvider.Impl();
sp.setNullValueSerializer(new NullSerializer());
this.setSerializerProvider(sp);
}
}
or specify it for some property using @JsonSerialize
annotation, e.g:
public class MyClass {
@JsonSerialize(nullsUsing = NullSerializer.class)
private String property;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…