I have a @RestController that returns net.sf.json.JSONObject:
@PostMapping("/list")
public JSONObject listStuff(HttpServletRequest inRequest, HttpServletResponse inResponse) {
JSONObject json = new JSONObject();
...
return json;
}
When JSONObject contains null reference, the following exception is thrown:
Could not write JSON: Object is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[\"list\"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject[\"object\"]->net.sf.json.JSONNull[\"empty\"])"
This is the legacy code that we are now cleaning up and at some point we will get rid of explicit JSON manipulations, but this will be a huge change, for now I would like to just get rid of the exception. I tried with following solutions:
- Define
Include.NON_NULL
in Spring's Object Mapper - so this piece of code in myWebMvcConfigurationSupportClass
:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
ObjectMapper webObjectMapper = objectMapper.copy();
webObjectMapper.setSerializationInclusion(Include.NON_NULL);
converters.add(new MappingJackson2HttpMessageConverter(webObjectMapper));
}
- Setting following property in application.yml:
spring.jackson.default-property-inclusion=non_null
- Checking the version of
com.fasterxml.jackson
- the only found in the dependency tree is 2.9.7.
None of the above helped.
Any suggestions on how to tell Spring to ignore null values in net.sf.json.JSONObjects?