[Solved] com.fasterxml.jackson.databind.exc. MismatchedInputException: Root name ‘ABC’ does not match expected (‘XYZ’) for type [simple type, class ClassName]


MismatchedInpuException is base class for all JsonMappingExceptions. It occurred when input is not mapping with target definition or mismatch between as required for fulfilling the deserialization. This exception is used for some input problems, but in most cases, there should be more explicit subtypes to use.

Example of MismatchedInputException

When we deserialize JSON data with property DeserializationFeature.UNWRAP_ROOT_VALUE then it will check root name as className. Here in this case class name “StudentDetail” while passing JSON having a root name as “student” that’s what throwing MismatchedInputException because of mismatch of root name.

public class StudentDetail {
	private int rollNumber;
	private String firstName;
	private String lastName;
	//getter and setter of class
	}
import java.io.IOException;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJacksonExample2 {

	public static void main(String[] args) {
	    String json="{\"student\":{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\"}}";

		try
		{
	    ObjectMapper mapper = new ObjectMapper();
	    //Before  deserialize this properties will unwrap the json.
	    mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);

	    StudentDetail student= mapper.readValue(json, StudentDetail.class);
	    System.out.println(student);
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}

Stacktrace of MismatchedInputException


com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'student' does not match expected ('StudentDetail') for type [simple type, class com.fiot.json.jackson.exceptions.StudentDetail]
 at [Source: (String)"{"Student":{"rollNumber":21 , "firstName":"Saurabh" , "lastName":"Gupta"}}"; line: 1, column: 2]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1356)
    at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:4087)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4011)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.fiot.json.jackson.exceptions.TestJacksonExample2.main(TestJacksonExample2.java:19)

Solution

To resolve this issue use the annotation @JsonRootName as below so that overwrite root name as “student” on default root name (StudentDetail)

@JsonRootName(value = "student")
public class StudentDetail {
	private int rollNumber;
	private String firstName;
	private String lastName;
	//getter and setter of class
	}

You would like to see

Follow below link to see more JSON issues solutions:

JSON Issues Solutions

References

https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/exc/MismatchedInputException.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s