Hi All,

Greeting of the day !.

Today we will look at how we can develop a simple REST API for performing CRUD operations with PostgreSQL and Spring Data JPA.

So let's start.

First of all, we will need PostgreSQL installed in our system, if you have not installed it yet you can refer to my below post for the installation guide and details of creating a database on the PostgreSQL server.

Download And Install PostgreSQL

Once you have installed PostgreSQL go to Spring Initializr and configure our spring boot application as shown below.


Next click on generate and one zip file of the project will be downloaded. Now unzip the project and open it in your IDE.

The next step is to add the PostgreSQL dependency so we will add the following dependency in pom.xml 

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.20</version>
</dependency>

Next, we will add our database-related properties to the application.properties as shown below.

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=add_user_name
spring.datasource.password=add_password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update

Now, let's configure the model entity class which will be used for creating tables by spring data JPA.

Student.java

Next, we create ResponseDTO which we will use to send a response from API to the caller.

ResponseDTO.java

Now we will create StudentDTO which will be used for getting the request payload and sending the response payload to the caller.
StudentDTO.java
Here we have not used validations if you want to learn how to validate REST API Requests refer to Spring Boot REST API Validation Using Annotations

Next, we will create a mapper class that will map StudentDTO to the Student entity class and vice versa.
StudentMapper.java

Now we will create a repository layer by extending JpaRepository which will provide us all inbuild methods used in CRUD operation.

StudentRepository.java

Now we will create a service layer which will be called by the controller and its responsibility is to manage transactions & we will also add all business logic in the service layer.

StudentService.java
 
Now let's create an implementation for StudentService in which we will implement all of the above methods that we have declared.

StudentServiceImpl.java

Next, we will create the exception class StudentNotFoundException which we have used in the above code in case we don't find students with the required ids.



Next, we will need to create a handler for our application that will catch all exceptions thrown from our code and will return a proper response to the API caller.


As a final step, we will create our controller which will have endpoints for CRUD operation, and from the controller, we will call StudentService as shown below.
API Would be using the following HTTP methods 

POST --Create Student

PUT --Update Student

GET--Find Student

GET-- List Student

DELETE --Delete Student

Below are details of the endpoint with payload for testing purposes.

Add Student--Http Method Post --End point -- localhost:8080/student

Payload 
{
    "name":"Akash",
    "mobileNumber":"123457"
}

List Student --Get End Point localhost:8080/student/list

Get Student --Http Method Get --End Point localhost:8080/student/1

Update Student --Http Method Put -- End Point localhost:8080/student

Payload 

{
    "id":1,
    "name":"Akash",
    "mobileNumber":"123457"
}

Delete Student --Http Method Delete --End Point localhost:8080/student/1

Also below is the attached project structure for your reference.



If you have any questions let me know in the comment section.

Thank you & Happy Learning.