Tag Archives: UnrecognizedpropertyException

[Solved] com.fasterxml.jackson.databind.exc. UnrecognizedPropertyException: Unrecognized field XYZ (class ABC), not marked as ignorable (X known properties: ])


UnrecognizedPropertyException occurred on time of deserialization of JSON. When no match of getter and setter method found for properties specified in JSON.

Jackson ObjectMapper required POJO class with respect to JSON to convert JSON to JAVA object.

UnrecognizedPropertyException Example

In this example to convert JSON to JAVA object , Student  class is not having any getter and setter method as basic requirement of POJO class. That’s causing the issue and throw UnrecognizedPropertyException.

public class Student {
private int rollNumber;
private String firstName;
private String lastName;
//no getter and setter for above properties
}
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJacksonExample {

public static void main(String[] args) {
   String json="{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\"}";
try
{
   ObjectMapper mapper = new ObjectMapper();
   mapper.reader().forType(Student.class).readValue(json);
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}

}

}

UnrecognizedPropertyException Stacktrace


com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "rollNumber" (class com.fiot.json.jackson.exceptions.Student), not marked as ignorable (0 known properties: ])
 at [Source: (String)"{"rollNumber":21 , "firstName":"Saurabh" , "lastName":"Gupta"}"; line: 1, column: 17] (through reference chain: com.fiot.json.jackson.exceptions.Student["rollNumber"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1611)
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1219)
at com.fiot.json.jackson.exceptions.TestJacksonExample.main(TestJacksonExample.java:15)

Solutions

There are couple of solutions to solve this issue:

  1. Jackson work with POJO classes, so write getter and setter of class properties to resolve above issue.
  2. Write @JsonIgnoreProperties(ignoreUnknown = true) on class level so that If any unknown property will receive in JSON that is not match with POJO object getter and setter then time of deserialization will ignore and no any exception occurred.
  3. For example : When you replace above JSON with this one as it’s having new property middleName which is not match with properties in Student POJO object. This will again through same exception with different exception message.

    String json="{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\", \"middleName\":\"Kumar\"}";
    

    No exception occurred when add annotation as @JsonIgnoreProperties(ignoreUnknown = true)

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Student {
    private int rollNumber;
    private String firstName;
    private String lastName;
    //write getter and setter of properties
    }
    
  4. To resolve this issue, you can also disable ObjectMapper checking for unknown properties.

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    

You would like to see

Follow below link to see more JSON issues solutions:

JSON Issues Solutions

 

Advertisements