Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no single-int-arg constructor/factory method

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?

like image 874
Chris Avatar asked Aug 07 '15 15:08

Chris


1 Answers

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.

like image 172
Mike Argyriou Avatar answered Sep 28 '22 06:09

Mike Argyriou