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!

asked Nov 26, 2018 at 15:33

1 Answer 1

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

answered Nov 26, 2018 at 15:37
Sign up to request clarification or add additional context in comments.

4 Comments

ParameterizedTypeReference is not being recognized by the IDE.
Check the doc docs.spring.io/spring-framework/docs/current/javadoc-api/org/…. Maybe the import statements are missing
ok i had to import it manually, can you tell what the method should be?, if i do this: restTemplate.getForObject(apiUrl , ParameterizedTypeReference<List<Someclass.class>>); An expression is expected, pointed by the IDE...
i could resolve it by following: stackoverflow.com/questions/23674046/… yonia's answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.