How to consume Restful web Service in a spring boot application

Tega Isiboge
6 min readMay 13, 2022

--

Objectives:

  • Consuming a remote rest API that returns a list of all countries
  • How to consume REST API using postman

Requirements:

  • A good understanding of Java is required.
  • Installed Java Development Kit (JDK)
  • Maven dependency manager is already installed
  • You need to know how to start a Spring boot project.
  • Installed IDE, either IntelliJ or Eclipse, however, for this course, we shall be using IntelliJ.
  • An Installed API testing tool but, for this article, we would be using Postman.

Let’s begin by starting a spring boot project.

Step 1: Via IntelliJ

  1. Launch your IntelliJ
  2. Open new from the file menu, click on the project and a new window will appear (as shown below), fill in the necessary information,
  • Name the project (I called mine ‘ConsumeApiApplication’)
  • Select the location where you want your project to be housed.
  • Select Java as the programming language
  • Select Maven as the project type.
  • You can call the Group anything (I called mine ‘com.tega’)
  • Let us refer to the Artifact as ConsumeApiApplication.
  • Using the dropdown list, select the Java SDK version.
  • Choose your Java version as well.
  • Our project will be packaged as a Jar file.
  • JAR is an abbreviation for Java ARchive. It is a ZIP file type that is used to combine several files into one.

Then, click on Next,

  1. Select the Spring Boot version (mine is 2.6.7).
  2. Select any other dependencies needed for your project. The appropriate Spring Boot starters will be inserted based on the dependencies specified in the pom.xml file.

Following the selection of these dependencies, click the Finish button as shown:

Let’s Understand the dependencies chosen;

Spring Web: Converts your project into a web application. The spring-boot-starter-web dependency transitively pulls in all web development dependencies. Spring MVC, REST, and Tomcat as the default embedded servers are used.

Spring Data JPA: This allows us to store data in SQL databases using Spring Data and Hibernate, a JPA implementation.

JPA is a Java EE (Enterprise Edition) specification that defines an API for Object-Relational Mappings (ORM) and managing persistent objects and relational databases. It is regarded as a standard method for Object Relational Mapping.

JPA, as a standard, does not perform any operations on its own. Hibernate is an ORM (Object Relational Mapping) tool that supports JPA.

Step 2: Using Spring Initializer (start.Spring.io):

Enter the Maven project properties, including dependencies, as shown above, then click the Generate button. This will create a zip file for you to download. Unzip it, then open it in your preferred code editor to sync the dependencies with Maven.

Maven Management

The dependencies and Maven plugins in our project are housed in the pom.xml file.

The dependency section contains the dependencies we added to the project, the dependencies are Spring Web and Spring Data JPA.

Below the dependencies are the plugins as seen below:

RUNNING A SPRINGBOOT APPLICATION

Recall that, the entry point to a Java Application is the main() (public static main void) method which is usually empty.

Whereas for Springboot projects, upon start-up, Spring boot creates the initial context and launches the Spring Boot application via the @SpringBootApplication annotation and a single line of code as shown below:

Next, we create a Rest Controller with the @RestController annotation and map API request.

@RestController was introduced in spring 4.0 to eliminate the need to annotate every request handling method of the controller class with @ResponseBody. So we just see @RestController above the class name as seen in line 12.

The @ResponseBody Spring annotation binds a method return value to the web response body. It is not recognized as a view name. Based on the content type in the request HTTP header, it employs HTTP Message converters to convert the return result to the HTTP response body.

Next, we see the Rest Template which is a spring class used to create applications that consume RESTful Web Services

Next, as seen in line 18, we saved the API to a variable (url). This is done to keep the code clean. If we had to write longer lines of code and the API was reusable, we’d just call it by its variable name instead of using the link everywhere.

Next, we use “countries” as the get mapping extension to make the external call.

The method get countries(), goes to the external API, and returns an array of all the countries stored in that API.

Note that in line 22, we made a call to the API by the variable name “url” rather than writing out the entire API there.

@Bean A bean is an object that is instantiated, assembled and managed by a Spring IoC container. When we annotate an object with @Bean, such object is created, managed and destroyed in Spring Container. We can inject an object into the Spring Container through the metadata(either xml or annotation), which is called inversion of control.

The @Autowired annotation is a feature of spring framework which enables one to inject the object dependency implicity. It internally uses setter or constructor injection. Autowiring can’t be used to inject primitive and string values. It works with references only.

Next

Run the application by pressing the play button as shown below:

Next, open the postman testing tool and do the following:

  1. Choose the GET as you are retrieving data not sending or deleting data.
  2. The port number should be in the console since the application has started running.

At the far right corner on the second line, the port number to be used is available (Mine is 3267).

  1. Followed by extension from @RequestMapping, followed by that from @GetMapping then press send.
  2. E.g localPort:3267/api/countries and you get a list of all the countries

Conclusion:

In this article we have shown how to consume a rest api in a spring boot application. We used the Rest Template library, which is a library that is used to consume Restful web services.

We have used IntelliJ IDE, for writing our small pieces of code, and running our spring boot application.

Other tools used, Postman, for testing API’s and documentation. However we used it for testing only.

I hope this has been helpful.

Please leave me a comment below if you might require some guidance using this guide..

--

--

Tega Isiboge
Tega Isiboge

Written by Tega Isiboge

A good blend of Software Engineering and Business Management Expertise, helping companies surpass their revenue goals, & achieve strategic business objectives.

No responses yet