Tag Archives: from string

[Solved] com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String “C21”: not a valid Integer value


InvalidFormatException is subclass of MismatchedInputException. InvalidFormatException occurred because of bad formatting or format not match with the presepecified format of value to deserialize.

InvalidFormatException Example

The issue is passing type is String while deserialization expecting as Int.

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

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\":\"C21\" , \"firstName\":\"Saurabh\" , \"lastName\":\"Gupta\"}";

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

	}

}

InvalidFormatException Stacktrace


{"rollNumber":"C21" , "firstName":"Saurabh" , "lastName":"Gupta"}
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "C21": not a valid Integer value
 at [Source: (String)"{"rollNumber":"C21" , "firstName":"Saurabh" , "lastName":"Gupta"}"; line: 1, column: 15] (through reference chain: com.fiot.json.jackson.exceptions.StudentDetail["rollNumber"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1549)
    at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:911)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer._parseInteger(NumberDeserializers.java:522)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:474)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:452)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    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:18)

Solutions

Pass value as the compatible type, here in this case number value roll number can’t accept as a text string with an alphanumeric.

In such cases, if your organization is using rollNumber as alphanumeric then convert int to String to make compatible. or ask your client application to pass the value of rollNumber as an int value.

References

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

You would like to see

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

Advertisements