no single-int-arg constructor/factory method

Chris picture Chris · Aug 7, 2015 · Viewed 15.1k times · Source

I have this code:

final Person p = new Person(1L);
final ObjectMapper mapper = JacksonUtil.INSTANCE.getMapper();
final TypeReference<HashMap<String, Object>> typeMap = new TypeReference<HashMap<String, Object>>() {};
final String personJson= mapper.writeValueAsString(p);
mapper.readValue(personJson, typeMap);

personJson is like:

"id" : 1

Whenever I have a Long type in my Json, it doesn't work when I try to read it. I have this error:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class org.codehaus.jackson.generated.java.lang.Number] from Integral number; no single-int-arg constructor/factory method

How can I make it accept the type Long? Is there any feature to enable in the mapper?

Answer

Mike Argyriou picture Mike Argyriou · Mar 8, 2016

Create a constructor in Person that accepts an integer (and not a long as you do). If you definitely want to accept a long then try to create a constructor that accepts a Number.