简体   繁体   中英

Deserialize response of JSON array in a class using POJO

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.8815675884918 8.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?

Your JSON example has an array. While you are trying to deserialize it into an object, BobSrmRicResponseBean which has a field ricResponse , which is not featured in the JSON. Either change your JSON to:

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

Or otherwise deserialize the list separately:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM