没有创建者,如默认构造,存在):不能从对象值反序列化(没有基于委托或属性的创建者 [英] No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator
问题描述
我正在尝试使用 Retrofit 和 Jackson 来使用 API 进行反序列化.我收到 onFailure 错误 No Creators, like default constructor, existing): cannot deserialize from Object value (no delegate- or property-based Creator
.
I am trying to consume an API using Retrofit and Jackson to deserialize. I am getting the onFailure error No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator
.
推荐答案
原因: 出现此错误是因为 jackson 库不知道如何创建没有空构造函数的模型,并且该模型包含带有参数的构造函数,这些参数未使用 @JsonProperty("field_name")
注释其参数.默认情况下,如果您没有向类添加构造函数,java 编译器会创建空的构造函数.
Reason: This error occurs because jackson library doesn't know how to create your model which doesn't have an empty constructor and the model contains constructor with parameters which didn't annotated its parameters with @JsonProperty("field_name")
. By default java compiler creates empty constructor if you didn't add constructor to your class.
解决方案:向您的模型添加一个空构造函数或使用 @JsonProperty("field_name")
Solution:
Add an empty constructor to your model or annotate constructor parameters with @JsonProperty("field_name")
如果您使用 Kotlin 数据类,那么也可以使用 @JsonProperty("field_name")
进行注释或注册 jackson 模块 kotlin 到 ObjectMapper
.
If you use a Kotlin data class then also can annotate with @JsonProperty("field_name")
or register jackson module kotlin to ObjectMapper
.
您可以使用 http://www.jsonschema2pojo.org/ 创建模型.
You can create your models using http://www.jsonschema2pojo.org/.
这篇关于没有创建者,如默认构造,存在):不能从对象值反序列化(没有基于委托或属性的创建者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!