Design a REST API with Spring Boot, JPA and MySQL
This article describes how to create a Rest API using Spring boot, JPA and MySQL database in a step by step guide.
Prerequisites
- Spring Tool Suite
- MySQL
- 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.
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:
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:
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.
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:
View all Donors:
Update a Donor:
Delete a Donor
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.