Cannot Convert JSON from Tiingo API to a Java object using RestTemplate

The RestTemplate is the central class within the Java Spring framework used for executing synchronous HTTP requests on the client-side.

While using RestTemplate, the below processes take place in the background and the user does not have to bother about it.

  • A URL object is created and a Connection to the API is established.
  • The HTTP request is Configured
  • Execution of the HTTP request in the background.
  • Response from the HTTP request conveyed back.
  • Conversion of HTTP response obtained into Java Object
  • Exception Handling.

There are various functions defined in Rest Template that are to convert JSON response to JAVA Objects.

  1. getForObject(url, classtype) – Used To retrieve a HTTP response representation by doing a GET on the URL. The response (if any) is converted based on the classType defined in the object definition.

  2. getForEntity(url, responseType) – Used to retrieve a HTTP response representation (if any) as ResponseEntity by doing a GET on the URL.

Below given is a General example of using a getForObject in Rest Template

ObjectMapper mapper = new ObjectMapper();   
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
List<POJO Class> collection = mapper.readValue(result, 
          new TypeReference<ArrayList<POJO Class>>() {
});

Note : You do not have to declare a POJO, the same has been created for you in the form of TiingoCandle.

The above example is used to convert the list of JSON objects from the URL Response into a List of Java Objects defined by the POJO class.

Here url is the HTTP call to the API.
The response obtained is converted to String.
Later the String Response is converted into an ArrayList of POJO objetcs

You can also refer to the following link for Other RestTemplate Methods.
https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/