Design a REST API with Spring Boot, JPA and MySQL

Somnath Musib
Code Fountain
Published in
4 min readMar 18, 2019

--

This article describes how to create a Rest API using Spring boot, JPA and MySQL database in a step by step guide.

Prerequisites

  1. Spring Tool Suite
  2. MySQL
  3. Postman

Preface

In this article, we will build a simple Rest API of a fictitious application called Donors. This application manages blood donors information and allows its users to Add a new donor, update existing donor information, view existing donors and delete a donor information from the application.

Step 1 : Set up the Environment

In STS, create a Spring boot project with web, MySQL and JPA dependencies.

Create Spring Boot Project in STS
Add JPA, MySQL and Web Dependencies

Browse to src/main/resources path and edit the application.properties with following contents:

spring.datasource.url=jdbc:mysql://localhost:3306/donors
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Note: It is assumed that you have installed MySQL and it is running in 3306 port with root user configured with password as password

Once the application is build, run the DonorsApplication to ensure set up is fine:

Run as Spring Boot App

If the set up is fine, following contents can be seen the console log:

2019–03–18 13:19:20.732 INFO 9860 — — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’
2019–03–18 13:19:20.738 INFO 9860 — — [ main] com.codefountain.base.DonorsApplication : Started DonorsApplication in 8.474 seconds (JVM running for 9.349)

Step 2 : Building the Application

In this application, we will have a model class called Donor, represents a blood donor and is having an Id, First Name and Last Name. There will be a controller DonorController to serve the requests and finally, DonorService as the DAO layer to communicate to the MySQL database.

Create following packages:

com.codefountain.base.model
com.codefountain.base.controller
com.codefountain.base.service

Crate the Donor class in model package, DonorController in controller package and DonorService in service package:

Donor.java

This class is the model object represents a Donor in the application:

Donor.Java

DonorController.Java

This rest controller severs the incoming requests. It facilitates create, read, update and delete requests. Create creates a new donor in the application, view displays all existing donors in the application, update updates an existing donor in the application and delete removes a donor from the system.

DonorService.java

This interface extends CrudRepository and provides support for DAO activities.

DonorService.java

Restart the application. Once the application is up and running, you can see the following in the log file:

Log in to MySQL console and you can see donor table has been created.

Step 3 : Testing the API

Open Postman and hit the following:

Create a donor:

Create a new Donor

View all Donors:

All available Donors

Update a Donor:

Updated the donor’s last name to Brown where Id is 3

Delete a Donor

Donor with Id 3 is deleted
Donor with Id 3 is deleted

Conclusion

In this article, we have created a simple Rest API with basic CRUD features. This demonstrates the usage of Spring Boot, JPA, MySQL and Rest Controllers.

--

--

Somnath Musib
Code Fountain

Software Developer, Cloud Architect | Author "Spring Boot In Practice" . Find more at https://musibs.github.io