no single-int-arg constructor/factory method

蓝咒 提交于 2019-12-10 15:18:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!