0

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!

Guille
  • 370
  • 2
  • 7
  • 21

1 Answers1

1

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

unnik
  • 1,123
  • 5
  • 18
  • 37
  • ParameterizedTypeReference is not being recognized by the IDE. – Guille Nov 26 '18 at 15:47
  • Check the doc https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/ParameterizedTypeReference.html. Maybe the import statements are missing – unnik Nov 26 '18 at 16:04
  • ok i had to import it manually, can you tell what the method should be?, if i do this: restTemplate.getForObject(apiUrl , ParameterizedTypeReference>); An expression is expected, pointed by the IDE... – Guille Nov 26 '18 at 16:31
  • 1
    i could resolve it by following: https://stackoverflow.com/questions/23674046/get-list-of-json-objects-with-spring-resttemplate yonia's answer – Guille Nov 26 '18 at 17:58