How to Create a REST API With Spring Boot

The acronym REST stands for REpresentational State Transfer, while API stands for Application Programming Interface. Together, they refer to a REST API. A REST API is a service that transfers requests and responses between two software systems, on a REST architecture.

The REST architecture builds web services that are accessible through URLs using one of four request verbs: POST, GET, PUT, and DELETE. So, you could say a REST API is software that allows you to create, read, update, and delete resources via URLs.

4

You can learn how to create a REST API using Spring Boot.

Initializing the Spring Boot Application

The first thing you should do isfamiliarize yourself with the basics of Springand set up a Spring Boot application. You’ll need to alter the dependencies, though. In addition to the web dependency, you’ll need to get the Spring Data Java Persistent API (JPA) dependency, and the driver for the database you intend to use (this application will use MySQL).

For this REST API, you will need a controller, a model, and a repository. So, the REST API will have the following file structure:

Spring RSET API

Creating the Model

The first class you’ll need to create is the customer model, which stores the data logic.

From the customer class above, you’ll see that each customer will have an id, name, and email. You’ll also notice several annotations that serve different purposes.

REST API file structure

Creating the Repository

This repository will allow you to interact with the customer data in the database.

The customer repository extendsSpring’s CrudRepositoy<T,ID>interface, passing it the Customer model class along with the type of the unique identifier for the entity, Integer.

REST API post header

The CrudRepository interface provides access to over 10 operations, including the generic CRUD methods you’ll need for the REST API. So, because the CrudRepository already defines the methods you’ll need, there’s no need to explicitly declare them in the CustomerRepository interface.

Creating the Controller

The controller allows you to update the data in your database using the model and the repository.

The controller above equips the REST API with CRUD operations, by using five of the CrudRepository<T,ID> interface methods (each assigned to a specific method). The controller also uses several important Spring annotations that allow it to perform its functions.

REST API post body

Connecting the Database to Your Application

To connect a database to any Spring application, you’ll need to use theapplication.propertiesfile under the resources folder. This file is initially empty, so you can populate it with the appropriate properties for the database that you intend to use. This application will use a MySQL database so, the application.properties file will contain the following data:

The data above shows that this application will be connecting to a MySQL database called onlineshopaholics, with a “root” username and “securepw” as the password. Your next step is to create the database and customer table in MySQL.

Creating Requests

There are many tools that you can use to test your REST API.Postman is a popular REST API testing tool, and you can use it to test the simple API you have built. After creating the MySQL table and running the Spring application, you can launch Postman and experiment with the four request verbs.

POST Request

This request will allow you to create new customers using the REST API. To complete this request, you’ll need to go to the headers section of your post request and create a new header (Content-Type). You should set this header’s value to application/json, as you’ll be creating new customers using JSON.

In the body of the request, you’ll need to change the type to raw and insert your JSON. Then you’ll need to insert the post URL:

Sending the request will return the following response:

You can see that the request was successful, and the new customer also has an id.

GET Request

Now that you have a customer, you can view it with the get request that returns all customers:

Or each customer by id:

PUT Request

it’s possible to update Janet with a new surname and email.

DELETE Request

you’re able to also delete Janet from the database.

Test Your Spring REST API Using JUnit

With Spring Boot, you may test any application (including REST APIs) using Spring’s test file. Software testing is important to Spring Boot. Each initialized Spring application uses JUnit for testing and allows you to send requests to your REST APIs.

Testing your code with a framework like JUnit gives peace of mind and improves your overall software development process.

These plugins will make you wonder why you used Photoshop in the first place.

Taming data is easier than it looks.

You’ve been quoting these famous films wrong all along!

I plugged random USB devices into my phone and was pleasantly surprised by how many actually worked.

Goodbye sending links via other apps.

Technology Explained

PC & Mobile