Collecting unmapped fields with Object Mapper

Jackson is a well-known Java library for converting JSONs into Java Objects. It maps JSON fields with object fields by name or mapping provided by @JsonProperty.

Jackson provides an annotation named @JsonAnySetter for collecting unmapped or unrecognized fields. This annotation is applied to a method having key-value pairs as arguments.

In action

Consider a class Profile that might have unmapped fields:

class Profile {

  String name;
  Map<String, String> unmappedFields = new HashMap<>();

  public void setName(String name) { = name;

  // for collecting unmapped field
  public void unmappedFields(String key, String value){
    // ... Do what you want
    unmappedFields.put(key, value);

Unmapped json fields (name and value) are received and stored in a collection via unmappedFields method.

/* Consider JSON
  "name": "m8ty",
  "handler": "@DM8tyProgrammer"
Profile p =, Profile.class);

// print "m8ty"

// print "{handler=@DM8tyProgrammer}"

handler field is not mapped to any Java Object field, so it is collected through JsonAnySetter.

Be Updated

Only articles are shared on Medium