I've a springboot application. I'm trying to replace null values with a custom string ("NA"). So, I've configured an object mapper with a custom serializer provider that in turn has a custom null value serializer. You can check the code below.
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper jacksonObjectMapper() {
return new CustomObjectMapper();
}
@Bean
public SerializationConfig serializationConfig() {
return jacksonObjectMapper().getSerializationConfig();
}
}
class CustomObjectMapper extends ObjectMapper {
CustomObjectMapper() {
super();
DefaultSerializerProvider.Impl serializerProvider = new DefaultSerializerProvider.Impl();
serializerProvider.setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString("NA");
}
});
this.setSerializerProvider(serializerProvider);
}
}
Now the problem is, even these beans are getting created, the custom null value serializer is still not taking effect. In the response, I'm still seeing lot of null values. Is there anything else I'm missing here?