How to handle the errors generated while deserializing a JSON in Java?

JavaJSONObject Oriented ProgrammingProgramming

The DeserializationProblemHandler class can be registered to get called when a potentially recoverable problem is encountered during the deserialization process. We can handle the errors generated while deserializing the JSON by implementing the handleUnknownProperty() method of DeserializationProblemHandler class.


public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOException


import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
public class DeserializationErrorTest {
   public static void main(String[] args) throws JsonMappingException, JsonGenerationException, IOException {
      String jsonString = "{\"id\":\"101\", \"name\":\"Ravi Chandra\", \"address\":\"Pune\", \"salary\":\"40000\" }";
      ObjectMapper objectMapper = new ObjectMapper();
      DeserializationProblemHandler deserializationProblemHandler = new UnMarshallingErrorHandler();
      Customer customer = objectMapper.readValue(jsonString, Customer.class);
// UnMarshallingErrorHandler class
class UnMarshallingErrorHandler extends DeserializationProblemHandler {
   public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser jp, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException {
      boolean result = false;
      super.handleUnknownProperty(ctxt, jp, deserializer, beanOrClass, propertyName);
      System.out.println("Property with name '" + propertyName + "' doesn't exist in Class of type '" + beanOrClass.getClass().getName() + "'");
      return true; // returns true to inform the deserialization process that we can handle the error and it can continue deserializing and returns false, if we want to stop the deserialization immediately.
// Customer class
class Customer {
   private int id;
   private String name;
   private String address;
   public int getId() {
      return id;
   public void setId(int id) { = id;
   public String getName() {
      return name;
   public void setName(String name) { = name;
   public String getAddress() {
      return address;
   public void setAddress(String address) {
      this.address = address;
   public String toString() {
      return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";

Output when true is returned

Property with name 'salary' doesn't exist in Class of type 'Customer'
Customer [id=101, name=Ravi Chandra, address=Pune]

Output when false is returned

Property with name 'salary' doesn't exist in Class of type 'Customer'
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "salary" (class Customer), not marked as ignorable (3 known properties: "id", "address", "name"])
at [Source: (String)"{"id":"101", "name":"Ravi Chandra", "address":"Pune", "salary":"40000" }"; line: 1, column: 65] (through reference chain: Customer["salary"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty( at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla( at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(
at com.fasterxml.jackson.databind.ObjectMapper.readValue(
at com.fasterxml.jackson.databind.ObjectMapper.readValue(
at DeserializationErrorTest.main(
Published on 29-Oct-2019 12:54:08