Guille
Guille

Reputation: 380

Java Spring JSON parse error: Cannot deserialize instance out of START_ARRAY token

I have a method with a restTemplate call like this:

restTemplate.getForObject(apiUrl ,Someclass.class);

Someclass.class:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Imp implements Serializable {
    @JsonProperty("Id")
    private String Id;

    @JsonProperty("ReportId")
    private String ReportId;

    @JsonProperty("Title")
    private String Title;

    @JsonProperty("Name")
    private String Name;

    @JsonProperty("Uri")
    private String Uri;

}

The API returns an array, and the error i'm receiving is: org.springframework.web.client.RestClientException: Error while extracting response for type [class ...] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of com... out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com... out of START_ARRAY token

Which restTempalte method shoud i use to get proper api response?, or where is the problem?.thanks!

Upvotes: 0

Views: 11411

Answers (1)

unnik
unnik

Reputation: 1153

You said the API returns an array.

But your line of code restTemplate.getForObject(apiUrl ,Someclass.class); will work only for a single Someclass object.

You should use new ParameterizedTypeReference<List<Someclass.class>> along with the exchange method.

Refer to the below link

Get list of JSON objects with Spring RestTemplate

Upvotes: 1

Related Questions