1

I have a bean like

class CustomResponse<Type> {
  String f1;
  String f2;
  List<Type> rows;
}

Here the Type can be a custom bean having Map inside it. In this case, I want to make sure to not print null values inside that map.

For example: My CustomResponse object can be new CustomResponse<Row>(); where Row is,

class Row {
  Map<String, Object> keyValues;
}

I am using Jackson 2.5 and using ObjectMapper like

ObjectMapper mapper = new ObjectMapper();
if (skipNullFields) {
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
}

Now if I write mapper.writeValueAsString(customResponseObj);, in the final response, I see null values inside the List of rows variable: rows. At the same time, if I write mapper.writeValueAsString(((Row)rows.get(0)).keyValues); I don't see null values in the final Json response - as I am directly passing a Map here instead of nesting it inside another bean.

Can you kindly help me to make sure I don't see null values even when I write as mapper.writeValueAsString(customResponseObj); ?

Note:

  1. I know this question is very similar to this post. But in that post, nesting a Map inside a bean doesn't seem to be handled.

  2. I am using new ObjectMapper instance for every thread. Hence I am not sharing a single instance of ObjectMapper across threads causing any thread safety issues.

sriram
  • 712
  • 8
  • 26

0 Answers0