我正在尝试使用Retrofit和Jackson来反序列化一个API。我得到的onFailure错误没有创造者,像默认构造,存在):不能从对象值反序列化(没有委托或基于属性的创造者。


当前回答

我有这个问题,我修复了下面的代码。

@Configuration
open class JacksonMapper {

    @Bean
    open fun mapper(): ObjectMapper {
        val mapper = ObjectMapper()
        ...

        mapper.registerModule(KotlinModule())
        return mapper
    }
}

其他回答

我知道这是一个老帖子,但对于任何使用Retrofit的人来说,这可能是非常有用的。

如果你正在使用Retrofit + Jackson + Kotlin + Data类,你需要:

添加实现组:com.fasterxml.jackson。,名称:' Jackson -module-kotlin',版本:'2.7.1-2'到你的依赖,这样Jackson可以反序列化到数据类 当构建翻新时,传递Kotlin Jackson Mapper,以便翻新使用正确的映射器,例如:

    val jsonMapper = com.fasterxml.jackson.module.kotlin.jacksonObjectMapper()

    val retrofit = Retrofit.Builder()
          ...
          .addConverterFactory(JacksonConverterFactory.create(jsonMapper))
          .build()

注意:如果没有使用Retrofit, @Jayson Minard有一个更一般的方法回答。

我使用rescu与Kotlin,并通过使用@ConstructorProperties解决它

    data class MyResponse @ConstructorProperties("message", "count") constructor(
        val message: String,
        val count: Int
    )

Jackson使用@ConstructorProperties。这应该修复Lombok @Data以及。

只需要添加@NoArgsConstructor就可以了。

我在这里搜索这个错误:

No Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator

与Retrofit无关,但如果您正在使用Jackson,则通过向抛出错误的类添加默认构造函数来解决此错误。 更多信息请点击:https://www.baeldung.com/jackson-exception

使用Lombok时要小心,尤其是@Builder。

解决这个问题的方法是:

   @JsonDeserialize(builder = X.XBuilder.class)
       class X{
          @JsonPOJOBuilder(withPrefix = "")
          public static class XBuilder{

          }
}

我希望它能让你的生活更轻松