Spring Boot – Spring Data JPA – Postgres Example

Building a sample RESTful API Service using Spring Boot, Spring Data JPA and a Postgres instance running in a Docker container

Overview

This tutorial covers all the required steps to build and run a Spring Boot and Spring Data JPA Backed RESTFul Web Service.

We will build a simple Spring Boot Application by adding all the required dependencies, create Entity Bean and a JPA Repository Interface, and create a Rest Controller that exposes standard GET, POST, PUT, and DELETE endpoints. For the persistence, we will use Postgres database instance by running it in a docker container locally.

Postgres in a Docker Container

Our application needs a locally. accessible Postgres database instance. If you already have one, you can skip this section.

A quickest way to launch a Postgres instance is to use Postgres Docker image (link). We’ll use docker-compose to download and launch postgres docker image in our local machine.

version: "3"

services:
  database:
    restart: always
    image: postgres:latest
    container_name: postgres-latest
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    ports:
      - 54032:5432
Code language: YAML (yaml)

Note that, we are using latest image of Postgres database. Also we are setting up a database name, user name, and a password. By default, Postgres listens on port 5432. However, we are forwarding the 5432 port of the container to port 54032 port of our local host.

~ docker-compose -f path/to/docker-compose.yml up -dCode language: Bash (bash)

Launch the Postgres container by executing docker-compose command.

Creating network "postgres-docker_default" with the default driver
Pulling database (postgres:latest)…
latest: Pulling from library/postgres
b4d181a07f80: Pull complete
46ca1d02c28c: Pull complete
a756866b5565: Pull complete
36c49e539e90: Pull complete
664019fbcaff: Pull complete
727aeee9c480: Pull complete
796589e6b223: Pull complete
add1501eead6: Pull complete
fdad1da42790: Pull complete
8c60ea65a035: Pull complete
ccdfdf5ee2b1: Pull complete
a3e1e8e2882e: Pull complete
a6032b436e45: Pull complete
Digest: sha256:2b87b5bb55589540f598df6ec5855e5c15dd13628230a689d46492c1d433c4df
Status: Downloaded newer image for postgres:latest
Creating postgres-latest … done

We can confirm the Postgress database is up and running in the docker container.

Spring Boot + Spring Data JPA + Postgres Application

Now, that our Postgres instance is up and running, we will now create our Spring Boot – Spring Data JPA – Postgres application. To do that, we will refer to CRUD REST Service With Spring Boot, Hibernate, and JPA tutorial. The tutorial demonstrates building a Spring Boot and Spring JPA application from scratch by detailing every step.

Having followed the tutorial we now have,

  • Spring Boot Web Application
  • All the required Maven/Gradle dependencies set. For example: spring-boot-starter-parent, spring-boot-starter-data-jpa, spring-boot-starter-web, and optionally lombok
  • An Entity Bean and a Repository Interface.
  • And, a Spring Rest Controller that exposes standard GET, POST, PUT, and DELETE endpoints.

We will now add Postgres Java Driver dependency to our application.

For Maven Project

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
</dependency>Code language: HTML, XML (xml)

Or, for a Gradle Project

runtimeOnly 'mysql:mysql-connector-java'Code language: Gradle (gradle)

Postgres Datasource Configuration

The only part we are left to do is adding datasource configuration for our Spring Boot project.

application.yml file

spring:
  datasource:
    driverClassName: "org.postgresql.Driver"
    url: "jdbc:postgresql://localhost:54032/postgres"
    username: "postgres"
    password: "password" ##Enter your root passwordCode language: YAML (yaml)

Note that we have provided the same port, database name, user, and password that we configured in the docker-compose.yml file. Also, we have provided the Postgres JDBC Driver class name.

Launch the Application

Now, it is time to launch our Spring Boot + Spring Data JPA + Postgres service. To do so, we can simply run the Application.java file or use maven Spring Boot plugin as shown.

~ mvn spring-boot:runCode language: CSS (css)

Doing this we should see, the project is built and and application started successfully.

INFO  o.s.b.w.e.t.TomcatWebServer:220 - Tomcat started on port(s): 8083 (http) with context path ''
INFO  c.a.s.d.Application:61 - Started Application in 8.652 seconds (JVM running for 9.468)
INFO  o.s.b.a.ApplicationAvailabilityBean:75 - Application availability state LivenessState changed to CORRECT
INFO  o.s.b.a.ApplicationAvailabilityBean:75 - Application availability state ReadinessState changed to ACCEPTING_TRAFFIC

Summary

In this tutorial we quickly built a Spring Boot and Spring Data JPA application that uses Postgres Database. We covered the steps to launch a Postgres instance in a Docker Container and configure a few essential details. As part of out application, we created a Spring Boot application and added required components like Controller, Entity Bean and JPA Repository interface.

For full source of the examples used here, please visit our Github Repo – spring-boot-data-jpa-postgres.