We need to fetch the GDP data from WorldBank API. As we discussed, it is REST end point, where we have to send few parameters and will get the response. For this, we will use RestTemplate to make REST call. The following is the definition for the com.packt.external.WorldBankApiClientclass, which is used to invoke the World Bank API and process its response to return List<CountryGDP>:
public List<CountryGDP> getGDP(String countryCode) throws ParseException { RestTemplate worldBankRestTmplt = new RestTemplate(); ResponseEntity<String> response = worldBankRestTmplt.getForEntity(String.format(GDP_URL, countryCode), String.class);
//the second element is the actual data and its an array of object JSONParser parser = new JSONParser(); JSONArray responseData = (JSONArray) parser.parse(response.getBody()); JSONArray countryDataArr = (JSONArray) responseData.get(1);
List<CountryGDP> data = new ArrayList<CountryGDP>(); JSONObject countryDataYearWise=null; for (int index=0; index < countryDataArr.size(); index++) { countryDataYearWise = (JSONObject) countryDataArr.get(index);