java - 无法反序列化 `java.util.ArrayList 的实例` 超出 START_OBJECT 令牌
问题描述
无法将 Json 反序列化为 List 集合。我正在使用 Lombok,它包含字段变量:
@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
private Integer success;
private ArrayList<String> data;
}
向 API 发布请求的 RestAssured 控制器:
public ValidatableResponse getFirstAparserStatus(AparsersDto toGetAparser) {
return RestAssured
.given().spec(getDefaultRequestSpecification())
.body(toGetAparser)
.when()
.post(apars141 + port1)
.then()
.log().all();
}
并提取帖子请求的正文:
private AparsersController aparsersController = new AparsersController();
public AparsersResponceDto postFirstBody(AparsersDto aparsersDto) {
return aparsersController
.getFirstAparserStatus(aparsersDto) //Sent post request with the body aparsersDto through RestAssured
.statusCode(200)
.extract().body().as(AparsersResponceDto.class); // Here i can't make extract of the 'data' field due to collection List.
}
响应Json:
"success": 1,
"data": {
"45.90.34.87:59219": [
"http"
],
"144.76.108.82:41049": [
"http"
],
"5.9.72.48:59473": [
"http"
],
"130.0.232.208:49327": [
"http"
],
"217.172.179.54:39492": [
"http"
],
...
完整的错误信息:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of START_OBJECT token at [Source: (StringReader); line: 1, column: 21] (through reference chain: com.rest.dto.AparsersResponceDto["data"])
我该如何解决?
解决方案
data
在 JSON 中不是一个,ArrayList<String>
而是一个Map<String,List<String>>
所以改变你的DTO如下。
@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
private Integer success;
private Map<String,List<String>> data;
}