javajacksondeserializationjackson-databind

Jackson deserialize on missing fields and capture them


I have requirement where we are calling an api and deserializing the json response into an object using Java. I am using jackson library to do so. There are 2 things needed:

  1. Ignore the missing fields while deserializing so that no exception is thrown when new fields are added by api provider
  2. Capture those missing fields and log them so that we can communicate with the api provider

I know how to deserialize json with missing field by simply marking object mapper to ignore missing fields or on class level but how can we achive capturing of these missing fields?


Solution

  • In your mapper use

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    

    In your pojo, add a field like below. This map will have all ignored properties after deserialization.

    @JsonAnyGetter
    @JsonAnySetter
    Map<String, Object> ignoredProperties = new LinkedHashMap<>();