-->
Skip to main content

Solution : com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field

com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field XXX not marked as ignorable

com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field

This is generic exception com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field XXX not marked as ignorable people face while parsing JSON string to a custom object. 

Exception Trace that comes is generally like

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "year" (class com.example.client.model.Vehicle), not marked as ignorable (4 known properties: "model", "vin", "make", "inventoryId"])
 at [Source: [{"inventoryId":14279520,"vin":"abcdefghijklmnop","make":"Toyota","model":"Sienna","year":2011}]; line: 1, column: 95] (through reference chain: java.util.ArrayList[0]->com.example.client.model.Vehicle["year"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:839) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1045) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1352) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1330) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:264) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736) [jackson-databind-2.6.3.jar:2.6.3] at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2745) [jackson-databind-2.6.3.jar:2.6.3] at
Today I face the same issue while parsing one of my JSON and scenario was I was expecting JSON to hold only 4 fields and I was consuming the same by creating Object on my side and then parsing it.

The object I have is : 
 public class Vehicle {
   private Long inventoryId;
   private String vin;
   private String make;
   private String model;
 }
and JSON that was coming earlier was a list of Vehicle Object with a response as below
 [
    {
        "inventoryId": 14279520,
        "vin": "abcdefghijklmnop",
"make": "Toyota", "model": "Sienna"
} ]
Earlier it was working fine until another developer changes the API and add a few more fields that I haven't handled at the side of the consumption of API.

So the new Json now I am getting with new field year added 
 [
    {
        "inventoryId": 14279520,
"vin": "abcdefghijklmnop",
"make": "Toyota", "model": "Sienna",
"year": "2015" } ]
So I have only one thing in mind that I have to update my Vehicle class to add extra field added in API. but then I was thinking what about in future someone else changes that again.

So this solution will not be a good solution for this so I googled and find out that we can ignore extra fields which are added later in two ways.

Now we will discuss both these ways 

Here are two ways to solve this exception com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field

1. Using @JsonIgnoreProperties(ignoreUnknown = true)

In this way, we just need to put this annotation on our Vehicle class in our case and it will start working successfully.

Here is the implementation code : 
 @JsonIgnoreProperties(ignoreUnknown = true)
 public class Vehicle {
   private Long inventoryId;
   private String vin;
   private String make;
   private String model;
 }

2. Setting FAIL_ON_UNKNOWN_PROPERTIES false or disable it

In this option, we simply set ObjectMapper property as below
 ObjectMapper objectMapper = new ObjectMapper();
 //either use this one
 objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
 //or use this one
 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Here is the full method code that is being used in our case
 private List<Vehicle> parseJsonToVehicleResponse(final String json) 
    throws CustomException {
  try {
      ObjectMapper objectMapper = new ObjectMapper();
      //either use this one
      objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
      //or use this one after commenting first and uncommenting it
      //objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      return objectMapper.readValue(json, new TypeReference<List<Vehicle>>() {});
  } catch (Exception e) {
    throw new CustomException(e);
  }
 }
That's it we solve our exception and this code is updated for Jackson library version 2.11.2.

Conclusion

In this tutorial, we have covered how to solve com.fasterxml.jackson.databind.exc.unrecognizedpropertyexception: unrecognized field exception using two ways

1. Using @JsonIgnoreProperties(ignoreUnknown = true) on class level.

2. Setting FAIL_ON_UNKNOWN_PROPERTIES false or disable it.

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

Comments