java 使用POJO反序列化类中JSON数组的响应

mbzjlibv  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(164)

I am trying to fetch the response of the JSON array using the below code. But I am getting com.fasterxml.jackson.databind.exc.MismatchedInputException. Below is my response.

[
    {
        "performanceId": "0P0I",
        "companyId": "007XQ",
        "investmentId": "F0VTK"
    }
]

Below is my code

public static ObjectMapper getJSONMapper() {
        objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }
ValidatableResponse response = given().header("Authorization", token)
                    .header("Content-type", "application/json").body(testCaseBean.getRequest()).when().log().all()
                    .post(EndPoint.SRM_BOB_RIC_API).then().log().all();

            response.assertThat().statusCode(200);
            actualRIOutput = CommonUtils.getJSONMapper()
                    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                    .readValue(response.extract().asString(), BoBSrmRicResponseBean.class);
            System.out.println(actualRIOutput.getRicResponse().get(0).getCompanyId());

I am getting the following error.
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type com.mtar.indexes.bean.BoBSrmRicResponseBean from Array value (token JsonToken.START_ARRAY ) at [Source: (String)"[{"performanceId":"0P00BI","companyId":"0C07XQ","investmentId":"F0SVTK"}]"; line: 1, column: 1] at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1601) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1375) at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1322) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:640) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:221) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:197) at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
The Bean I am constructing is as follows.

@Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public class RICMappingResponseBean {
        private String performanceId;
        private String companyId;
        private String investmentId;
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class BoBSrmRicResponseBean {
    List<RICMappingResponseBean> ricResponse;
}

Am I missing something here?

3z6pesqy

3z6pesqy1#

您的JSON示例包含一个数组。当您尝试将其反序列化为对象BobSrmRicResponseBean时,该对象包含一个字段ricResponse,而该字段在JSON中不起作用。请将您的JSON更改为:

{
  "ricResponse": [  {
        "performanceId": "0P0I",
        "companyId": "007XQ",
        "investmentId": "F0VTK"
    } ]
}

或以其他方式单独反序列化列表:

ObjectMapper mapper = CommonUtils.getJSONMapper()                    
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

 CollectionType javaType = mapper.getTypeFactory()
      .constructCollectionType(List.class, RICMappingResponseBean.class);

 List<RICMappingResponseBean> list = mapper.readValue(response.extract().asString(), javaType);

相关问题