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


当前回答

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

@Configuration
open class JacksonMapper {

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

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

其他回答

我用龙目岛。我得到的错误,因为我没有把@NoArgsConstructor在我的模型类。

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

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

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

在下面的用例中遇到相同的错误。

我尝试使用sprint引导(2.0.0快照版本)在各自的bean中没有默认构造函数的情况下达到Rest(Put映射)端点。

但是在最新的Spring Boot版本(2.4.1版本)中,同一段代码可以正常运行。

因此在最新版本的Spring Boot中不再需要bean默认构造函数

我在这里搜索这个错误:

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 = StationResponse.StationResponseBuilder.class)
 public class StationResponse{
   //define required properties 
 }     

 @JsonIgnoreProperties(ignoreUnknown = true)
 @JsonPOJOBuilder(withPrefix = "")
 public static class StationResponseBuilder {}

参考:https://projectlombok.org/features/Builder与杰克逊