Error: Com.Fasterxml.Jackson.Databind.Exc.MismatchedInputException!

This Exception usually occurs when input is not mapping with target definition or mismatch occurs between the inputs as required for fulfilling the deserialization.

This exception is used for some input problems.

One of the possible reasons that this error is raised is because there is more than one object in the JSON file.

And hence the object mapper function is not able to map the value from JSON to the POJO Object.

For example:

Consider the following JSON
[
{
“rollNumber”:21,
“Name”:“A”,
}
{
“rollNumber”:22,
“Name”:“B”,
}
]

The Required POJO for the JSON file is

public class StudentDetail {
    private int rollNumber;
    private String Name;
    
    // Respective getter and setter of class
}

Now to map the JSON to JAVA object, the Mapper function is used,

ObjectMapper map = new ObjectMapper();
StudentDetail student = mapper.readValue(json, StudentDetail.class)

Where StudentDetail is the POJO class.

student is the instance of the POJO class.

This issue can be solved by using an array of Instances for the POJO class.

You can refer to the following link to understand as to Multiple Instances in Java works.

Working with multiple instances of a Java Class

The final result from the object mapper would be.

StudentDetail [] student = mapper.readValue(json, StudentDetail[].class)