0

ClassCastException while parsing Google Vision API Response

REQUEST

visionApiRequest {"requests":[{"features":[{"maxResults":2,"type":"LOGO_DETECTION"}],"image":{"source":{"imageUri":"https://www.tenfold.com/wp-content/uploads/2017/05/icon-sap-hybris.png"}}}]}

RESPONSE

visionApiResponse {  "responses": [    {      "logoAnnotations": [        {          "mid": "/m/0gwz218",          "description": "Hybris",          "score": 0.17361198,          "boundingPoly": {            "vertices": [              {                "x": 65,                "y": 58              },              {                "x": 114,                "y": 58              },              {                "x": 114,                "y": 120              },              {                "x": 65,                "y": 120              }            ]          }        }      ]    }  ]}

Parsing code snippet:

String resp = "";
    while (httpResponseScanner.hasNext()) {
        final String line = httpResponseScanner.nextLine();
        resp += line;
        //System.out.println(line); // alternatively, print the line of response
    }
    System.out.println("visionApiResponse "+resp);


    BatchAnnotateImagesResponse annotateImagesResponse = new ObjectMapper().readValue(resp, BatchAnnotateImagesResponse.class);

    List<AnnotateImageResponse> responses = annotateImagesResponse.getResponses();

    for (AnnotateImageResponse res : responses) {

        System.out.println(res);
    }

Exception at line : for (AnnotateImageResponse res : responses) {

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.google.api.services.vision.v1.model.AnnotateImageResponse

Dependency : google-api-services-vision-v1-rev370-1.23.0.jar

How to handle this ?

ashish
  • 161
  • 1
  • 6
  • 13

1 Answers1

0

Resolved using com.fasterxml.jackson.core.type.TypeReference

List<AnnotateImageResponse> responses = mapper.convertValue(annotateImagesResponse.getResponses(), new TypeReference<List<AnnotateImageResponse>>() {
    });
ashish
  • 161
  • 1
  • 6
  • 13