Call another rest api from own rest api in spring boot application

10,893

Solution 1

Yes the spring boot provides a way to hit an external URL from your app via a RestTemplate. Below is a sample implementation of getting the response as string or you can also use a data structure of desired choice depending on the response,

@RestController
@RequestMapping("/myapi/ver1")
public class Table1Controller {

   @Autowired
   private RestTemplate restTemplate;

   @GetMapping("/table1data")
   public String getFromUrl() throws JsonProcessingException {
        return restTemplate.getForObject("https://services.odata.org/V3/Northwind/Northwind.svc/Customers",
            String.class);
   }
}

You can create a config class to define the Bean for the rest controller. Below is the snippet,

@Configuration
public class ApplicationConfig{

   @Bean
   public RestTemplate restTemplate() {
       return new RestTemplate();
   }

}

Solution 2

You can use RestTemplate for third party API call and return the response from your API

final String uri = "https://services.odata.org/V3/Northwind/Northwind.svc/Customers";

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class);

This website has some nice examples for using spring's RestTemplate

Solution 3

Create a @Bean of RestTemplate

 @Bean
 public RestTemplate restTemplate() {
     return new RestTemplate();
 }

By using the above RestTemplate you can fetch the data from ur own localhost

  String url = "https://services.odata.org/V3/Northwind/Northwind.svc/Customers";
  restTemplate.getForObject(url,String.class);
Share:
10,893
Mirza Bilal
Author by

Mirza Bilal

Updated on June 06, 2022

Comments

  • Mirza Bilal
    Mirza Bilal almost 2 years

    I am learning Spring Boot, I have managed to deploy an API on my computer which fetches data from Oracle and when I paste the link http://localhost:8080/myapi/ver1/table1data in browser it returns me the data. Below is my controller code :

    @CrossOrigin(origins = "http://localhost:8080")
    @RestController
    @RequestMapping("/myapi/ver1")
    public class Table1Controller {
    
    
        @Autowired
        private ITable1Repository table1Repository;
    
        @GetMapping("/table1data")
        public List<Table1Entity> getAllTable1Data() {
            return table1Repository.findAll();
        }
    

    Now this scenario is working fine. I want to do another thing. There is an API https://services.odata.org/V3/Northwind/Northwind.svc/Customers which returns some customers data. Does spring boot provide any way so that I can re-host/re-deploy this API from my own controller such that instead of hitting this the above link in the browser, I should hit http://localhost:8080/myapi/ver1/table1data and it will return me the same customers data.

    • Anil Nivargi
      Anil Nivargi about 4 years
      Using RestTemplate, we can call external rest api from current api.
  • 123
    123 about 4 years
    Unnecessary autowiring of rest template.
  • Vignesh T I
    Vignesh T I about 4 years
    Yea, that is a snippet to show a RestTemplate. To avoid the autowire we can use like this, RestTemplate restTemplate = new RestTemplate()
  • Mirza Bilal
    Mirza Bilal about 4 years
    Sir you are right, your solution is best approach but the above answer met my exact requirements completely as was asked in the question. Your answer returned me the address in string but the selected answer also showed me how to redirect to that particular address (which was exactly what i required). Hope you will understand :) By the way I thank you for your help too sir