没有single-int-arg构造函数/工厂方法 [英] no single-int-arg constructor/factory method

查看:195
本文介绍了没有single-int-arg构造函数/工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

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 如:

"id" : 1

每当我在Json中有一个 Long类型时,当我尝试读取它时它不起作用。我有这个错误:

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:
不能实例化类型的值[simple type ,类
org.codehaus.jackson.generated.java.lang.Number]来自积分号;
没有单个int-arg构造函数/工厂方法

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

如何让它接受类型?是否在mapper中启用了哪些功能?

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

推荐答案

在Person中创建一个接受整数的构造函数(而不是长整型)你做)。如果你肯定想接受long,那么尝试创建一个接受Number的构造函数。

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.

这篇关于没有single-int-arg构造函数/工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆