Better design for nested REST call in Rest Template

by hades   Last Updated October 19, 2019 19:05 PM

I not sure whether this question should be asked here, let me know if it is not.

I have this application where i need to call 3rd party APIs using Java's RestTemplate, first i call Country API, base on the code returned, i call Vehicle API, and so on. I can do this with nested API call, but i am thinking, is there any better design?

The following is just a simple example:

ResponseEntity<Country> countryResponse= rest.exchange("https://localhost:8989/api/countries/1",HttpMethod.GET, new HttpEntity<>,Country.class);

Country countryBody=  countryResponse.getBody();

if(countryBody!= null){
    ResponseEntity<Vehicle> vehicleResponse = rest.exchange("https://localhost:8989/api/countries/"+countryBody.getCode()+"/vehicles",HttpMethod.GET, new HttpEntity<>,Vehicle.class);

  ...with the result returned by vehicleResponse, keep nesting...
}
Tags : java rest spring


Related Questions


Updated August 28, 2019 19:05 PM

Updated March 25, 2019 02:05 AM

Updated April 16, 2019 12:05 PM

Updated April 27, 2015 00:02 AM