1. Overview

This article contains spring boot restful web services with JPA example. We have used MySQL as a database in this example. Spring boot is the first choice to develop Rest Service with JSON. Spring boot makes fast development using easy configuration with minimum requirements. Nowadays Rest API development is very popular because of microservice architecture application development. We have explained this example with different layers like Controller, Service, and Data Access Object layers which are best practice to write API or web application.

2. Example

This example contains Rest Web service CRUD operation with JPA MySQL database. An example is a consist of Controllers which will handle all HTTP request, Service layer will help us to write business logic and DAO or Repository which will communicate with the database and persistence of data. In this example, we have used MySQL as a database.

Spring boot restful web services with JPA example

2.1 pom.xml

spring-boot-starter-data-jpa used for spring boot JPA support.

mysql-connector-java used for the MySQL connection, we may replace any other database connection drivers to change any other database like Postgres, Oracle or any other JPA supported database.

2.4 EmployeeController

@RestController will generate a response in JSON format. @RestController is a combination of @Controller + @ResponseBody.

@RequestMapping will handle the specific request, value contains URL path, a method can be GET, POST, DELETE or any other method will is supported by HTTP.

While return List<Employee> or only Employee object then spring boot automatically convert to JSON Array and JSON respectively so we do not worry about to convert objects to JSON which will be managed by spring boot.

2.6 EmployeeServiceImpl

Here is actually an implementation of EmployeeService interface, We are just performing CRUD operation so we do not have too much logic for business but we need to write actually business logic over here.

2.7 EmployeeDAO

Here is Data Access Object, It will communicate with database and convert SQL query result to POJO Class or Model Class. Please note that EmployeDAO is an interface, Then we should have one question how a query will be generated:

Spring boot automatically generate a query from method name here is

findAll query will be “SELECT * FROM EMPLOYEE“

findEmployeeByEmployeeRole query will be “SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ROLE =: ROLE“

1. Add or Save Employee localhost:8080/save with FORM data.

2. list or fetch all Employee. localhost:8080/listEmployee

3. Delete Employee using localhost:8080/delete?id=18

Spring boot Restful web services with JPA example – Delete Employee

3. Conclusion

In this article, We have learned that spring boot makes our ways easy to build Rest JSON or XML based web services. We have used postman tool to call JSON services. The code has been written in multiple layers so we can reuse of code and easy to understand for other developers also. We can also secure our web services, here is an example of how to secure web services

JavaDeveloperZone is the group of innovative software developers. We are expert in Java JEE and BigData application development. Our contributions will help Java developers and make development journey easy. Feel free to ask any question and suggestion. Always have space for improvement !
We are also providing software application development as service. Contact us