Abhishek Singh
9 min readFeb 8

--

In this blog we are going to learn how to make CRUD operation using Spring Boot and JPA , JPA provides methods to perform operations like Create, Read, Update and Delete(CRUD)

In this blog you will learn :

  • What is a REST API?
  • Basics of designing REST API
  • How to configure Spring Data, JPA, Hibernate to work with Databases
  • How to use Spring Data JPA to interact with MySQL Database
  • How to define Data Models Layer, Service Layer and Repository interfaces i.e Data Layer
  • How to create Spring Rest Controller to process HTTP requests
  • How to execute different kinds of REST API with Postman?

So lets get started :

API Overview :

We will be building a Spring Boot JPA Rest CRUD API for a Employee application where :

  • Each employee will be having employee id, first name, last name, email and their salary
  • This will help us to create, retrieve, update and delete employees

These are APIs endpoints or http methods that we will create to manage employees :

  • Create a new employee : @PostMapping("/api/v1/emp")
  • Retrieve all employees : @GetMapping("/api/v1/emp")
  • Get details of a specific employee : @GetMapping("/api/v1/emp/{id}")
  • Update employee details : @PutMapping("/api/v1/emp/{id}")
  • Delete a employee: @DeleteMapping("/api/v1/emp/{id}")

So lets get started :

First we will dig into What is Api?

API is a set of services available for performing certain task, API enables two software components to communicate with each other using a set of definitions and protocols, APIs are intended to be used by programmers not by end user, API stands for Application Programming Interface in which Application refers to any software with distinct function and Interface is the contract of service between two application or two software components and for Web APIs we have SOAP APIs that uses Simple Object Access Protocol and client and server exchange messages using XML and it is a less flexible API that was more popular in the past, Now the REST APIs are the most popular and flexible API found on the web today and clients and servers exchange data via web services using HTTP with Json as the primary data format and the types of APIs by scope of use and some other examples of apis are :

Java Stream API is the java programming language API that allows us to use collections of data structures easily and flexibly, Java Servlet API is the Software Libraries API that enables programmers to build web applications running on the Java platform, JDBC API is the remote API that enables Java applications to talk to remote database servers.

What is a REST API?

REST stands for Representational State Transfer and REST is not a protocol or standard It’s a set of architectural constraints, REST was created by computer scientist Roy Fielding in 2000 in his doctoral research and as compared to a SOAP that uses XML messaging, REST APIs are easier to
use, faster and more lightweight and have better scalability.

And note that the main feature of rest API is statelessness and that’s why it is called representational state transfer and a request is self descriptive and has enough context for the server to possess it and servers do not save any data related to client requests and no server side sessions are required and each request is separate and unconnected and statelessness makes the rest API have better scalability.

  • Basics of designing a REST API :

Rest and Http :

REST is built on top of HTTP (Hypertext Transfer Protocol)

HTTP is the language of the web, HTTP has a few important verbs

  • POST : Create a new resource
  • GET : Read a resource
  • PUT : Update an existing resource
  • DELETE : Delete a resource

HTTP defines standard response codes :

  • 200 : SUCCESS
  • 404 : RESOURCE NOT FOUND
  • 400 : BAD REQUEST
  • 201 : CREATED
  • 401 : UNAUTHORIZED
  • 500 : INTERNAL SERVER ERROR

How to configure Spring Data, JPA, Hibernate to work with Database :

Lets now get started by creating a project so to create project i will be using Spring Initializr — Spring Boot tool so now go to https://start.spring.io/ and create a simple project like this :

Project Created Using Spring initializr

Now click on GENERATE and it will create a spring boot maven project with all the selected dependencies, now in the next step import this maven project in your favorite IDE i will be using STS ide you can use any ide of your choice..

Now open the pom.xml file and you will be able to see all the info and dependencies which you provided during project creation and it will look like this :

pom.xml :

  • How to use Spring Data JPA to interact with MySQL Database

Now open application.properties file and provide the following details:

  • How to define Data Models Layer, Service Layer and Repository interfaces i.e Data Layer

Now the next step is to create the models or entity so create a simple java Employee class like this and provide all the annotations like this which will be used by JPA to connect with database tables :

Employee.java

Now lets define the Employee Repository :

EmployeeRepository.java

Now lets define the Employee Service Layer with all the methods :

EmployeeService.java

  • How to create Spring Rest Controller to process HTTP requests :

EmployeeController.java

How to handle Exception if any unwanted data is sent from client to the server so here comes the exception handling so we will create like this :

  • How to execute different kinds of REST API with Postman?
  • Create a new employee : @PostMapping("/api/v1/emp")
  • Retrieve all employees : @GetMapping("/api/v1/emp")
  • Get details of a specific employee : @GetMapping("/api/v1/emp/{id}")
  • Update employee details : @PutMapping("/api/v1/emp/{id}")
  • Delete a employee: @DeleteMapping("/api/v1/emp/{id}")

Now is the time to test the api which we have created.

Open the postman client :

first we will create a employee :

Create a new employee : @PostMapping("/api/v1/emp")

choose POST from the menu and provide this url http://localhost:8080/api/v1/emp and in body section select raw and provide employee details in json format

Now you see the status code is 200 OK it means this employee data is persisted in database..

Now we will retrieve all the employees from the database :

  • Retrieve all employees : @GetMapping("/api/v1/emp")

Now we will get details of specific employee :

  • Get details of a specific employee : @GetMapping("/api/emp/{id}")

Now we will update a employee details : here i am updating lastname and salary of the employee

  • Update employee details : @PutMapping("/api/v1/emp/{id}")

Now in the final step we will be performing delete operation on the resource so we will be deleting a employee :

Delete a employee: @DeleteMapping("/api/v1/emp/{id}")

And wow you got the message like Employee with id : 1 has been deleted

that’s cool right so how simple it is to create CRUD operation on a resource and we can expose all the endpoints using spring data rest and jpa..

Thats all in this blog see you in the next blog till then Happy Learning!

You can check my git repo here : https://github.com/abhishek621

--

--

Abhishek Singh

Engineer | Blogger | Traveller