Java – Error reading entity from input stream – with Jackson

Error reading entity from input stream – with Jackson… here is a solution to the problem.

Error reading entity from input stream – with Jackson

I NEED TO CALL AN API TO RETURN THE FOLLOWING IN JSON FORMAT AND CONVERT THEM TO JAVA OBJECTS:

GET /api/myimpl/?ids=5,12

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": "A1",
        "impl": "HeatSmart"
    },
    {
        "id": "B2",
        "impl": "My String"
    }
]

I have the following classes:

public class QueryStrings {
    private String id;
    private String impl;

public QueryStrings() {
    }

public QueryStrings(String id, String impl) {
        this.id = id;
        this.impl = impl;
    }

public String getId() {
        return id;
    }

public String getImpl() {
        return impl;
    }

public void setId(String id) {
        this.id = id;
    }

public void setImpl(String impl) {
        this.impl = impl;
    }
}

In my code, I do the following:

client = ClientBuilder.newClient().register(JacksonFeature.class);
Response clientResponse = client.target(myURL)
        .queryParam("ids", "5,12")
        .request()
        .accept(MediaType.APPLICATION_JSON_TYPE)
        .get();
QueryStrings jsonResponse = clientResponse.readEntity(QueryStrings.class);

clientResponse returns status OK. However, when I try to put json into the object I want, the last line returns an error:

javax.ws.rs.ProcessingException: Error reading entity from input stream.

What’s missing from my code?

Solution

Your api returns an array of QueryStrings, so you can use GenericType in readEntity

List<QueryStrings> results = response.readEntity(new GenericType<List<QueryStrings>>(){});

Related Problems and Solutions