(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘0.97982854’)

  Kiến thức lập trình

how are you? I have some questions about this error with Springbot Java 17 that appears in the console and that I have not been able to resolve.
I am making a simple request to a service which returns this json

{
    "id": "81a51bdd-0088-4d03-9c2b-b0cade46e2b0",
    "project": "cb965621-e344-48ea-a8e4-251376ce116b",
    "iteration": "30a03b97-1269-44b2-a2c0-d95212b82d42",
    "created": "2024-05-16T14:45:32.686Z",
    "predictions": [
        {
            "probability": 0.97982854,
            "tagId": "d79f6f4a-a95d-4293-944d-aa13f96e8198",
            "tagName": "empty shelf"
        },
        {
            "probability": 0.01930153,
            "tagId": "fcc2eae3-b7f7-466f-8ed8-e256d658a755",
            "tagName": "Full shelf"
        },
        {
            "probability": 0.00086999265,
            "tagId": "f3662445-f00d-4d54-a4e9-43c855653e5e",
            "tagName": "It is not a winery"
        }
    ]
}

For this case I only work with the predictions array where inside you get probability and tagName.
But when I want to obtain the values I get the following in the console.

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.example.prueba.model.Predictions (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘0.97982854’)
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 359] (through reference chain: com.example.prueba.model.CompuVisionResponse[“predictions”]->java.util.ArrayList[0])

This is my class code

@Data
@AllArgsConstructor
public class CompuVisionResponse {
   List<Predictions> predictions;
}
#######
@Data
@AllArgsConstructor
public class Predictions {
     double probability;
     String tagName;
}
#######
@Data
public class DataBodega {
     double probability;
     String tagName;
}

This is the method where I call the API to obtain the data

 @Override
    public List<List<DataBodega>> getDataBodegaModulo1(List<String> urlBodegas) {
        List<List<DataBodega>> dataListBodegas = new ArrayList<>();
        List<DataBodega> o;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(Constants.PREDICTIONS_KEY, subscriptionkey);

        for (String url: urlBodegas){
                RequestCompuVisionModel requestModel = new RequestCompuVisionModel();
                requestModel.setURL(url);
                log.info("KEY: " + subscriptionkey);

                HttpEntity<RequestCompuVisionModel> requestEntity = new HttpEntity<>(requestModel, headers);
                System.out.println("Data String.class: "+ restTemplate.exchange(compuVision1Url, HttpMethod.POST, requestEntity, String.class));
                ResponseEntity<CompuVisionResponse> response = restTemplate.exchange(compuVision1Url, HttpMethod.POST, requestEntity,CompuVisionResponse.class);

                CompuVisionResponse data = response.getBody();

                o = getData(data);
                dataListBodegas.add(o);

            }
        return dataListBodegas;
    }

    public List<DataBodega> getData(CompuVisionResponse compuVisionResponse) {
        List<Predictions> predictions = compuVisionResponse.getPredictions();
        List<DataBodega> dataListBodegas = new ArrayList<>();

        for (Predictions p : predictions) {
            DataBodega dataBodega = new DataBodega();
            dataBodega.setProbability(p.getProbability());
            dataBodega.setTagName(p.getTagName());

            dataListBodegas.add(dataBodega);
        }
        return dataListBodegas;
    }

The method always collapses when reaching
ResponseEntity<CompuVisionResponse> response = restTemplate.exchange(compuVision1Url, HttpMethod.POST, requestEntity,CompuVisionResponse.class);

I have printed the response through the console but as a String.class and it does bring me the data

Data String.class: <200 OK OK,{“id”:”616f37b2-acab-44ce-82cd-05d20c79ae50″,”project”:”cb965621-e344-48ea-a8e4-251376ce116b”,”iteration”:”30a03b97-1269-44b2-a2c0-d95212b82d42″,”created”:”2024-05-16T17:06:34.065Z”,”predictions”:[{“probability”:0.97982854,”tagId”:”d79f6f4a-a95d-4293-944d-aa13f96e8198″,”tagName”:”empty shelf”},{“probability”:0.01930153,”tagId”:”fcc2eae3-b7f7-466f-8ed8-e256d658a755″,”tagName”:”Full shelf”},{“probability”:0.00086999265,”tagId”:”f3662445-f00d-4d54-a4e9-43c855653e5e”,”tagName”:”It is not a winery”}]},[Content-Length:”500″, Content-Type:”application/json; charset=utf-8″, request-id:”616f37b2-acab-44ce-82cd-05d20c79ae50″, x-envoy-upstream-service-time:”755″, apim-request-id:”616f37b2-acab-44ce-82cd-05d20c79ae50″, Strict-Transport-Security:”max-age=31536000; includeSubDomains; preload”, x-content-type-options:”nosniff”, x-ms-region:”East US”, Date:”Thu, 16 May 2024 17:06:33 GMT”]>

If anyone knows what it could be, I would be eternally grateful.

I hope I can get the data and store it in my variables but I get a big problem in the console that I can’t solve

New contributor

Alejandro Huamaní Reátegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT