Simple Rest API With SpringBoot, Postgres and Docker

In this tutorial, we are going to use,

  1. SpringBoot
  2. Postgres
  3. Docker
  4. Docker compose

1. Create SpringBoot Application

This is the pom.xml. All the dependencies are included here.

2. SpringBoot Rest API

This is the Employee model. After that this model is going to be an Entity in the database.

@ Entity- Class is an entity and it is mapped to the database.

@ Table- This annotation specifies the name of the table in the database.

@ Id- This specifies the primary key of the table.

@ GeneratedValue- This annotation is defined the strategy of primary key generation.

@ Column- This annotation specifies the name of the column with mapping the attribute of the model.

This module deals with enhanced support for JPA based data access layers. There are various spring data repositories such as JpaRepository, CrudRepository and etc.

In here, I am going to use JpaRepository. JpaRepository is extended by CrudRepository and PagingAndSortingRepository.

@ Repository- This annotation is used to indicate the data access component in the persistence layer.(all the database actions such as save, delete, update , find and etc)

This is the way to create custom exception.

A controller contains the business logic of an application. Here we are using @ RestController annotation to implement Rest based API.

@ RestController- This is the controller to use for creating Rest API.

@ Autowired- Marks a constructor, field, or setter method to be autowired by Spring dependency injection.

@ RequestMapping- maps HTTP request with a path to a controller method.

@ GetMapping- the path of the endpoint to perform a Get method.

@ PostMapping- the path of the endpoint to perform a Post method.

@ PutMapping- the path of the endpoint to perform a Put method.

@ DeleteMapping- the path of the endpoint to perform a Delete method.

@ PathVariable- extract any value which is embedded in the url.

@ RequestBody- bind the HTTP request body with the domain object.

This is the Main class. @ SpringBootApplication annotation is come up with @ EnableAutoConfiguration, @ ComponentScan and @ Configuration annotation.

@ EnableAutoConfiguration- enables Spring Boot’s autoconfiguration mechanism to import important modules for the Spring Boot to run.

@ Component- Tells the compiler that the following class is a component which should be included when compiling the whole application.

@ ComponentScan- This one does the Scan of which packages we are going to use in the following Java class.

@ Configuration- allow to register extra beans in the context or import additional configuration classes

After that we use mvn clean package command to build the jar file.

This jar file contains embedded tomcat. So we can this as a normal java application.

In here jar file is created under target file.

embedded tomcat server

3. Dockerize SpringBoot Application

FROM openjdk:8
ADD target/first-0.0.1-SNAPSHOT.jar first-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "first-0.0.1-SNAPSHOT.jar"]

4. Use Postgres docker

docker hub

First we should pull the postgres docker using below command.

docker pull postgres:9.5

5. Use docker-compose

docker compose is a tool for defining and running multi-container Docker applications.

version: "3"
network_mode: bridge
container_name: postgres
- postgres-data:/var/lib/postgresql/data
- 5432
- 5432:5432
- POSTGRES_DB=docker
restart: unless-stopped
# This is my rest api app*****************************************
network_mode: bridge
container_name: myfirst
- 8080
- 8081:8080
- /home/pramod/Desktop:/var/tmp
restart: unless-stopped

This is the command to run docker compose.

docker-compose up

After this, postgres and rest api application dockers are up and running.

6. logs

logging.file = /var/tmp/mylog.log

we are going to mount this file to location of my machine. It is defined under myfirstapp configuration in docker-compose file.

- /home/pramod/Desktop:/var/tmp

7. Rest APIs

1. Save employee

2. Get all employees

3. Get employee by id

4. Display error when trying to get not existing id

5. Update employee

6. Delete employee

8. Check The Entities

docker exec -it postgres /bin/bash

within the docker container, we need to use psql command like this. Here I am using name of the database as “docker”.

psql -d docker

9. Resources