For example CRUD with Spring Boot, PostgreSQL, JPA, Hibernate RESTful API

Tram Ho

In this article, I will show you how to configure Spring Boot using PostgreSQL database and build RESTful CRUD API from scratch.

We will also learn how Spring Data JPA and Hibernate can be used with PostgreSQL.

I will write REST APIs for Q&A application. The Q&A application will have 2 domain models: Question and Answer. Because a question will have many answers , I will define the one-to-many between the Question and the Answer entity.

First, I will create the Project and configure PostgreSQL database. Later, I will define domain models and repositories to access data from PostgreSQL. Finally, I will write REST APIs and test APIs using Postman.

I. Initializing Project

In this article, I will initialize the project using the Spring Initializr web tool, follow the instructions below:

First, visit http://start.spring.io

  • Enter the postgres-demo into the Artifact field.
  • Add Web, JPA, PostgreSQL and Lombok in the Dependencies section.
  • Click Generate Project to download the project.

Please import the project into IDE you like and start working.

Once the directory structure of the project is completed, you can refer to it to create packages and classes accordingly.

II. PostgreSQL configuration

In order for Spring Boot to use PostgreSQL as a data source, we must configure it by adding driver, url, username and password of PostgreQuery database to src/main/resources/application.properties :

III. Identify domain models

Domain models are classes that are mapped to the corresponding tables in the database. We will have two main domain models in our application, Question and Answer . Both domain models will have the same properties as createAt and updateAt . It is best to separate these fields into a separate class. We will create an abstract class called AuditModel to contain these fields. We will also use the JPA Auditing feature of Spring Boot to automatically fill createAt and updateAt .

1. AuditModel

The following AuditModel class will be extended by other entities. It will use annotation @EntityListeners(AuditingEntityListener.class) to automatically fill createAt and updateAt .

  • Note: I am using Lombok's @Getter and @Setter , You need to set your IDE to be able to use Lombok. In case you get an error, please refer to the setting here

Enable JPA Auditing

To enable JPA Auditing, we will need to add @EnableJpaAuditing annotation to one of the config classes. So, open class PostgresDemoApplication.java and add @EnableJpaAuditing annotation as follows:

2. Question model

Below is class Question entity. It is mapped to a table named questions in the database.

3. Answer model

Below is the Answer entity class. It contains an @ManyToOne annotation that declares it has a one-to-one relationship with Question entity.

IV. Definition of Repositories classes

The Repository will be used to access Question and Answer from the database.

1. QuestionRepository

2. AnswerRepository

V. Building the REST APIs

Finally, let us write the REST APIs inside the controllers to perform CRUD operations for Question and Answer .

1. QuestionController

2. AnswerController

Custom ResourceNotFoundException class

Question and Answer REST APIs will return ResourceNotFoundException when no questions found or answers in the database. Below is the definition of ResourceNotFoundException class.

The exception class contains an @ResponseStatus(HttpStatus.NOT_FOUND) annotation @ResponseStatus(HttpStatus.NOT_FOUND) to alert Spring Boot status of 404 NOT FOUND when this exception is thrown.

VI. Run the application and test the API through Postman

We have completed the REST API building. Run the application and test those APIs. The following screenshots will show you how to test the API with Postman.

1. Create a question: Post / questions

2. Get question information: Get / questions

3. Create an answer: Post / questions / {questionId} / answers

4. Get the Answer information of Question: Get / questions / {questionId} / answers

Share the news now

Source : Viblo