Avatar ·

Jackson/Kotlin: 不存在像默认构造函数这样的创建者 — 反序列化错误

大家好!我是 Kotlin 新手,正在尝试使用 Retrofit + Jackson 在 Android 中解析 JSON。我有一个简单的 data class: ```kotlin data class User( val id: Int, val name: String, val email: String ) ``` 以及 Retrofit 接口: ```kotlin interface ApiService { @GET("users/{id}") suspend fun getUser(@Path("id") id: Int): User } ``` 但在尝试执行请求时,我遇到了异常: ``` com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.app.User` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (BufferedInputStream); line: 1, column: 2] ``` 我通过 `jackson-module-kotlin` 模块使用 Jackson。问题出在哪里?我已经定义了包含所有字段的 data class,并且使用了 val,为什么 Jackson 无法创建对象?我尝试添加 @JsonProperty — 没有帮助。请告诉我如何正确配置。