Tag Archives: MismatchedInputException

[Solved] com.fasterxml.jackson.databind.exc. MismatchedInputException: Cannot deserialize instance of `XYZ` out of START_ARRAY token


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

Here MismatchedInpuException issue is happening because of type mismatch because passing JSON as an array of StudentDetail while deserialization is mapped for single StudentDetail object.

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.ObjectMapper;

public class TestJacksonExample2 {

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

	    System.out.println(json);
		try
		{
	    ObjectMapper mapper = new ObjectMapper();
		//issue is here
	    StudentDetail student= mapper.readValue(json, StudentDetail.class);
	    System.out.println(student);
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}

}

MismatchedInputException Stacktrace


 [{"rollNumber":21 , "firstName":"Saurabh" , "lastName":"Gupta"}, {"rollNumber":22 , "firstName":"Abhishek" , "lastName":"Garg"}]
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.fiot.json.jackson.exceptions.StudentDetail` out of START_ARRAY token
 at [Source: (String)" [{"rollNumber":21 , "firstName":"Saurabh" , "lastName":"Gupta"}, {"rollNumber":22 , "firstName":"Abhishek" , "lastName":"Garg"}]"; line: 1, column: 2]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1461)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:185)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.fiot.json.jackson.exceptions.TestJacksonExample2.main(TestJacksonExample2.java:22)

Solutions

To resolve this issue, change the type from StudentDetail to as array StudentDetail[]

public static void main(String[] args) {
	    String json=" ["
	    				+ "{\"rollNumber\":21 , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\"}, "
	    				+ "{\"rollNumber\":22 , \"firstName\":\"Abhishek\" , \"lastName\":\"Garg\"}"
	    		+ "]";
	    System.out.println(json);
		try
		{
	    ObjectMapper mapper = new ObjectMapper();
		//Convereted to Type as array
	    StudentDetail[] students= mapper.readValue(json,  StudentDetail[].class );
	    System.out.println(students);
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}

References

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

You would like to see

Follow below link to learn more on JSON and  JSON issues solutions:

Advertisements

[Solved] com.fasterxml.jackson.databind.exc. MismatchedInputException: Cannot construct instance of `XYZ` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor


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.

MismatchedInputException Example

Here MismatchedInputException is occurring because of StudentDetail class define as private but Jackson always looks for class POJO for deserialization.

package com.fiot.json.jackson.annotations;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fiot.json.jackson.pojo.Address;
import com.fiot.json.jackson.pojo.Student;

public class JsonIgnoreUnknownProperties {

	public static void main(String[] args) {

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

	System.out.println(json);
	try {
		// ObjectMapper new instance
		ObjectMapper objectMapper = new ObjectMapper();
		//convert json data to respective java object
		StudentDetail student = objectMapper.readValue(json, StudentDetail.class);
		// print java student java object
		System.out.println(student);
	} catch (JsonMappingException ex) {
		ex.printStackTrace();
	} catch (JsonGenerationException ex) {
		ex.printStackTrace();
	} catch (IOException ex) {
		ex.printStackTrace();
	}
	}
	@JsonIgnoreProperties(ignoreUnknown = true)
	private class StudentDetail
	{
			@JsonProperty(value = "id")
			private int rollNumber;

			@JsonProperty(value = "firstName")
			private String firstName;

			@JsonProperty(value = "lastName")
			private String lastName;

			//Getter and Setter

	}
}

Stacktrace of Exception


com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.fiot.json.jackson.annotations.JsonIgnoreUnknownProperties$StudentDetail` (although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor
 at [Source: (String)"  {"id" : 11,"firstName" : "Saurabh","middleName" : "Kumar","lastName" : "Gupta",}"; line: 1, column: 3]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1032)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.fiot.json.jackson.annotations.JsonIgnoreUnknownProperties.main(JsonIgnoreUnknownProperties.java:27)

Solutions

As solution of this exception for current issue define StudentClass as public because Jackson looks for POJO class for deserialization.

As per definition of POJO (Plain Old Java Object) is the class with out any restriction, having getter and setter methods, if required can also implement object class methods like equals, toString() etc.

You would like to see

Follow below link to see more JSON issues solutions:

JSON Issues Solutions

References

https://stackoverflow.com/questions/43923914/com-fasterxml-jackson-databind-exc-mismatchedinputexception-can-not-deserialize