当前位置: 首页 > 知识库问答 >
问题:

无法从START_OBJECT标记反序列化`java.util.ArrayList`的实例

蒙经纶
2023-03-14

无法将Json反序列化为列表集合。我使用的是Lombok,它保存字段变量:

@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
  private Integer success;
  private ArrayList<String> data;
}
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. 
}
"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"])

我该怎么修好它?

共有1个答案

吕新
2023-03-14

JSON中的data不是arraylist 而是map

因此,按以下方式更改DTO。

@Data
@Builder
@EqualsAndHashCode(exclude = "success")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AparsersResponceDto {
   private Integer success;
   private Map<String,List<String>> data;
}
 类似资料: