How to Call or Consume External API in Spring Boot?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because it is a rapid production-ready environment that enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Here we will learn how to create a Spring Boot application that will consume external API. We will be going through the following stages during this course:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- Create Spring Boot Project.
- Create Rest Controllers and map API requests.
- Build and run the Project.
- Make a call to external API services and test it.
Now let us automate every step to detail with the help of visual aid so that
Step 1: Creating Spring Boot project
First, visit the website and create a spring boot project. You can add another dependency also using the Dependencies section. Here we don’t need any additional dependencies.
Remember: One can download the spring boot project by clicking on Generate tab.
Once the project is downloaded import it into your Eclipse IDE, then the sample will look like as follows:
Note: pom.xml contains all the dependencies which are needed for your projects and will look like as given below:
Step 2: Create Rest Controllers and map API requests
Now, let us create one sample rest API call in our Rest Controller class. This call will simply return “Hello world” as output. We will use this API and call it in our same application.
Step 3: Build and run the Project
We can check the output by starting our spring boot application.
Now open postman and send GET request for URL as specified: http://localhost:8080/hello
Our Sample “hello” API service is working on localhost: 8080. Now we will see how to call this service using the Rest template.
Step 4: Make a call to external API services and test it
Here do make sure before making calls to external API, first, we will see what is Rest template is which is described below:
The Rest Template is the central Spring class used to create applications that consume RESTful Web Services. You can use the methods available in the Rest Template class to consume the web services for all HTTP methods.
- Here we have given GetMapping as “callclienthello” so that external call will be available on that extension.
- Next, Define your client URI on which your Service is accessible.
- Create Rest Template object, then Using getForObject method you can call the defined service.
- Now our “hello” service will also be available on extension “callclienthello”.
- Let’s test it and the output is generated as follows:
As you can see we are getting the same output that we were getting for our sample “hello” API call. Now Let’s see another example in which we will call external API which is available over the internet. For example “https://restcountries.eu/rest/v2/all”.If we now send requests for a given Countries API , then it will return list of countries then we will test it from Postman as depicted in below media
Now, we will call this countries API from our spring boot application.
Here I have given Get Mapping as “countries” so that We can make an external call using that extension. We have defined an external API with which we want to make calls in URL. Let’s see the output in postman by sending a request:
As you can see here, Countries API i.e “https://restcountries.eu/rest/v2/all”, Now available on extension “countries”. In this way, you can make calls to external API services using Spring Rest Template class.