Creating RESTful CRUD API with Spring Boot, PostgreSQL, JPA

In this article we’re going to create Customer Management System. We’ll build CRUD Restful API’s.

I’ll try to explain everything along the way. I’m using Ubuntu 18.04 and Intellij IDE.

Please make sure needed tools installed. If Postresql is not installed you can install it from here for ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04

I’ll not go into install and setup details. Let’s navigate to https://start.spring.io/ I’m going to use java 11, project maven and spring boot 2.4.3 versions. Let’s start to add dependencies we need for this project. After we added Spring web, Spring data JPA, Postresql Driver and Spring Data Elastic Search, click to generate and download the zip file.

Now our project is created and let’s open it in the IDE. When we first open it intellij will ask to import default scripts (if not installed might ask to download) choose import and let’s inspect pom.xml file

We see the dependencies we added while creating the project on spring inializr

Before start, we’re going to use Model,View, Controller (MVC) Software Architectural design, I know you’re wondering now, What is MVC?

There’s many ways to implement MVC so,I’ll explain itfrom my point of view.MVC,

  • Seperates application functionality and logic
  • organized programming
  • make easier to work especially with a team

Model

  • Responsible for getting and manipulating the data
  • Interacts with database
  • Communicates with the Controller
  • Can update the view

View

  • What the user sees at the end (UI)
  • Communicates with the controller
  • Consist of (usually) html,css

Controller

  • Receives input (view, url)
  • Process Requests(Get, Put,Delete,Post)
  • Gets data from the model
  • update the view

First step is to configure PostgreSQL to use it as database source.Open application.properties file and add the following

Note: If you’re using ubuntu and if you’re going to use postgres for the first time you need to configure postgresql settings and set a password as postgres in order to connect to the database and make this code above work for you. I set my password as postgres Check the link: https://docs.boundlessgeo.com/suite/1.1.1/dataadmin/pgGettingStarted/firstconnect.html

Now let’s create database in the postgreSql database server. open command line and type:

sudo -u postgres psql

we are connected to postgres it’s a default one and create table named Customers. Enter: CREATE DATABASE customers;

if you get result as CREATE DATABASE as the following then it’s successful.

To connect to database we created in that case it’s customers we type:

\c customers;

You are now connected to database “customers” as user “postgres”.
customers=#

Now our database created. Next step is to define domain Entity (Customer.java)

Create new package named model and then right click to model and create new class named Customer. Time to add some properties to our customer model. It’s simple POJO class.

add the attributes and generate constructer then getter and setter, the last thing to generate equals method and let’s add the annoations

The relationship between Customer and Order models is, OneToMany-ManyToOne Annoations. Basically, one customer can have one or many orders, many orders or one order owned by one customer.Now we can start implementing Order model which should be added in model package.

Create new package and name it as Repository. Let’s implement interfaces named as CustomerRepository then OrderRepository and extend it to JpaRepository as the following

CustomerRepository

OrderRepository

We’re ready to create our controllers. Create new package name controller and we’ll create new class named CustomerController it’ll include the following mapping for requests => Get, Post, Put,Delete

CustomerController

similar way for the OrderController

now we can run the application. The application started successfully and if we navigate to http://localhost:8080/api/v1/customers we’ll see an empty array.

and test it via postman or intellij. start postman and test as in the picture

Post request is succeed and if you refresh the page you’ll see there’s information as [{“id”:3,”fullName”:”hasan”,”email”:”hasan.cobanoglu1@outlook.com.tr”,”orders”:[]}

if we sent Delete request on http://localhost:8080/api/v1/customers/3 and refresh the page, it’s gone…

I hope you enjoyed this writing. If you have any questions or would like to give feedback, feel free to reach out.

source code:https://github.com/hasancbngl/CustomerManagementSystem/tree/master/Customer%20Management%20System

Life-long learner, Developer and traveler.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store