问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/31881975/no-single-int-arg-constructor-factory-method