REST API using Spring Boot and PostgreSQL with my learning experience…

Manojlakshan
5 min readMay 21, 2021

Let's start with my backstory. I am a SE undergraduate student. I needed to learn spring-boot for my academic assessment project. In that assessment, I had to make a REST API using spring-boot. So I followed some tutorials to learn spring boot. it is easy to pick up. simply, In spring-boot, we must construct our functionalities and place required annotations in the appropriate locations.

So in here, I am sharing what I learned and my experience with this superhero. you can get the source code from this

OVERVIEW

In this tutorial, I am going to build an API using spring-boot and postgresql. So this tutorial has three parts,

  1. STEP 01 — create psql database
  2. STEP 02 — initialize a project and build the API
  3. STEP 03 — Test the API using postman

PREREQUISITES

Java ( version 8+)

PostgreSQL

Java IDLE ( I prefer IntelliJ)

Postman (or any APi client)

STEP 01

Open your terminal by hitting ctrl + alt + T and type sudo -i -u postgres and hit the Enter then it will ask for your password. Then type psql and you will able to get a screen like this.

Now we are going to create a database to store our data. You need to hit createdb student to create the database name student. The hit \l to show all PostgreSQL databases in our machine and hit \c student to checkout to student database.

STEP 02

In this step, we are going to create our spring boot project. we need to set up our project with all dependencies that we want by using spring initializer easily.

We need to add Spring web, Spring data JPA and PostgreSQL driver as our dependencies. If you have already set up the project you can find the dependency by simple google search and copy-paste to the pom.xml file in your project.

Now click on GENERATE button or hit ctrl + enter to download our project. Extract the downloaded project and open it with your favourite IDLE. I use IntelliJ IDLE rest of this tutorial.

This is the basic project structure of our project. So the pom.xml file has included all dependencies. The pom.xml file is just like the package.json file in node. We have to add our code under the src folder.

There is a built-in basic application up. You can run it using RUN button in Intellij or run command mvn spring-boot: run on terminal.

Let’s jump to coding,

Go to the application.properties file under the resources folder and copy-paste these code snippets and add your information to it.

spring.datasource.url=jdbc:postgresql://localhost:5432/student
spring.datasource.username=your username
spring.datasource.password=your password
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
server.error.include-message=always

All right now we are going to implement our functionalities.

Lets code…

Now create a package in com.StudentTest.student and name it as models. We are going to build models of our objects that we are going to store in our database. Create a file and name it as Student.java and add some attributes and its getters and setter also.

This how our Students.java looks like with the annotations.

Let's move to make our StudentRepository interface under the Repository package. So I add a query to search student using his email. Keep in mind we use JPA built in the repository by extending our StudentRepository.

I add some customer query to find the student by his email.

Create a package and name it as services and add StudentSerives.java file to it. This provides the functionalities to the rest controller.

Now we are going to make our rest controller/ API under the controller package. First of all, we need to plan out endpoints and what these are going to do so.

We should have endpoints to add student, get all students, delete student and update student details.

We can create our endpoints as follows,

Greate now we have our REST API on our hand

Now we are going to test our API using postman. Before doing the test we need to run our spring boot API by run command mvn spring-boot : run on your terminal. You can see our API up and running in the tomcat server on port 8080.

STEP 03

Test our API

add a student to our database,

we should send student details with the request body.

Get all student in our database,

update student details,

Delete student from our database,

I have added this project to GitHub, you can find the git repository from this

Thank you for reading, I hope you gather some knowledge about build API using Spring boot and PostgreSQL.

--

--