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


当前回答

扩展Yoni Gibbs的回答,如果你在一个android项目中使用改装和配置序列化与Jackson,你可以做这些事情,以便反序列化工作预期与kotlin的数据类。

在你的build gradle导入中:

implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.+"

然后,你的改造实施:

val serverURL = "http://localhost:8080/api/v1"

val objectMapper = ObjectMapper()
objectMapper.registerModule(KotlinModule())
//Only if you are using Java 8's Time API too, require jackson-datatype-jsr310
objectMapper.registerModule(JavaTimeModule())

Retrofit.Builder()
    .baseUrl(serverURL)
    .client(
        OkHttpClient.Builder()
           .readTimeout(1, TimeUnit.MINUTES)//No mandatory
            .connectTimeout(1, TimeUnit.MINUTES)//No mandatory
            .addInterceptor(UnauthorizedHandler())//No mandatory
            .build())
    .addConverterFactory(
                JacksonConverterFactory.create(objectMapper)
            )
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build()

数据类:

@JsonIgnoreProperties(ignoreUnknown = true)
data class Task(val id: Int,
                val name: String,
                @JsonSerialize(using = LocalDateTimeSerializer::class)
                @JsonDeserialize(using = LocalDateTimeDeserializer::class)
                val specificDate: LocalDateTime?,
                var completed: Boolean,
                val archived: Boolean,
                val taskListId: UUID?

其他回答

在类中添加构造函数 或 如果您正在使用pojo添加 @NoArgsConstructor @AllArgsConstructor

还要包括JsonProperty - @JsonProperty("name") private List<员工>姓名;

当您使用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与杰克逊

扩展Yoni Gibbs的回答,如果你在一个android项目中使用改装和配置序列化与Jackson,你可以做这些事情,以便反序列化工作预期与kotlin的数据类。

在你的build gradle导入中:

implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.11.+"

然后,你的改造实施:

val serverURL = "http://localhost:8080/api/v1"

val objectMapper = ObjectMapper()
objectMapper.registerModule(KotlinModule())
//Only if you are using Java 8's Time API too, require jackson-datatype-jsr310
objectMapper.registerModule(JavaTimeModule())

Retrofit.Builder()
    .baseUrl(serverURL)
    .client(
        OkHttpClient.Builder()
           .readTimeout(1, TimeUnit.MINUTES)//No mandatory
            .connectTimeout(1, TimeUnit.MINUTES)//No mandatory
            .addInterceptor(UnauthorizedHandler())//No mandatory
            .build())
    .addConverterFactory(
                JacksonConverterFactory.create(objectMapper)
            )
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build()

数据类:

@JsonIgnoreProperties(ignoreUnknown = true)
data class Task(val id: Int,
                val name: String,
                @JsonSerialize(using = LocalDateTimeSerializer::class)
                @JsonDeserialize(using = LocalDateTimeDeserializer::class)
                val specificDate: LocalDateTime?,
                var completed: Boolean,
                val archived: Boolean,
                val taskListId: UUID?

如果你使用Unirest作为http库,使用GsonObjectMapper而不是JacksonObjectMapper也可以工作。

<!-- https://mvnrepository.com/artifact/com.konghq/unirest-object-mappers-gson -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-object-mappers-gson</artifactId>
    <version>2.3.17</version>
</dependency>
Unirest.config().objectMapper = GsonObjectMapper()

我通过添加一个无参数的构造函数解决了这个问题。如果你正在使用Lombok,你只需要添加@NoArgsConstructor注释:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
public class User {
    private Long userId;
    private String shortName;
}