RoadToWebDev Day#8- Building REST APIs with Spring Boot

This is a part of my ongoing series about exploring the Road To Web Development, you can find rest of the blogs here

In this article, we will see how to build a sample Spring Boot project using IntelliJ.

The overview of this project is to create and view a list of Songs. We’ll be using SongRepository to maintain a list containing objects of the class Song, and build REST methods on top of it to modify the same. I have tried to explain the code in a detailed manner.

I will be explaining each and every step from starting a “New project” on IntelliJ to sending the requests through Postman.

Step 1 :

Start a new project with Spring Initializr.

The Spring Initializr is ultimately a web application that can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code with.

Spring Initializr is used to generate a project structure with the following files:

1. A build configuration file, for example, build.gradle for Gradle or pom.xml for Maven.

2. A class with the main() method to bootstrap the application. (Bootstrapping in application terms means loading the application initially or starting an app.)

3. An empty JUnit test class. (A JUnit test is a method contained in a class that is only used for testing. This is called a Test class.)

4. An empty Spring application file: application.properties(Properties files are used to keep the’n’ number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application.properties file under the classpath.)

Set up the project JDK. It is better to download the latest JDK version before continuing the project.

Step 2:

Here we can select Group and Artifact ID for the new project, for now keeping the next settings as default.

Step 3:

Add the required dependencies required for your Spring project. Here we’ll be using ‘Spring Web’ under ‘Web’.

Starter of Spring web uses Spring MVC, REST, and Tomcat as a default embedded server. The single spring-boot-starter-web dependency transitively pulls in all dependencies related to web development. It also reduces the build dependency count.

Step 4:

Give your required project name and click Finish.

Step 5: Code!

Spring Boot provides the basic code given below at the start of any project. As we can see SpringApplication.run() method is already coded by the Spring Boot. This method starts the whole Spring Framework. All the methods to modify the SongRepository List got correctAll the methods to modify the SongRepository List got correct

The above code was already provided by the Spring Boot Initializr.

Let’s make a class named Song with common song related properties. Also, it would contain all the constructors, getters, and setters for all the defined properties of the Song object.

IntelliJ provides us plenty of inbuilt functions of predefined code.

So we do not need to type each and every constructor, getter, or setter rather, we just have to search for them(for search: double shift); select the required variables, and click OK, and its done. It’s easy right!

Creating Getters

Here is the code for the Song class:

Code for the class ‘Song’

Now we are constructing a repository which is basically a way to interact with a list of songs. We will invoke operations of this repository via GET and POST methods.

Code for the class ‘SongRepository’

Until now, we have made a basic class Song that represents all their properties and a repository class for storing all the values.

Now It’s time to make a controller class that would help us to use the SongRepository as per the need.

For using the SongRepository list, I have made three different APIs for different operations:

  1. GET Method for getting the list of songs present from the Songs List.(path = “/song”)
  2. POST Method to append only one object of the class Song in the Songs List. (path = “/song”)
  3. POST Method to append an entire list of songs to the Songs List. (path =“/songs”)

Run the program.

Once the server starts running, we can send the requests through the browser or Postman.

For the browser: http://localhost:8080/songs/

Postman is a software development tool that enables people to test calls to APIs. Postman users enter data, the data is sent to a special web server address, typically information is returned, which Postman presents to the user.

For sending requests through Postman:

  1. Checking if everything is working:

As we know that we have not added anything in the list yet. So it should return an empty list.

For sending GET request, select the GET option; check if the path is http://localhost:8080/song/, and Send! It will look like this:

Operating GET Method

2. For appending a single Song object into the list, we will now perform the POST method.

Change the method to POST. Change from Params to Body and click raw. Change Text to JSON. Set the path to http://localhost:8080/song/ And then you will be able to enter the content. Here we are entering one Song object as shown below and Send.

After entering the first object into the list, we can check if it is correctly appended to the list by performing a GET method again as below:

3. For appending an entire list to the current SongRepository list, change the specifications as said above and change the path to http://localhost:8080/songs/ and Send.

POST method for appending a list to the SongRepository

Let’s perform the GET method to check the appended list.

DONE! We are now able to add songs to a list and view them on demand.

Here I am sharing the git repository link of the same project:

Hope you understood the article!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store