Java Spring Boot REST Server with PostgreSQL
đź•“ 45 minutes
#
What you’ll learnHow to setup your application for :
- connecting to the PostgreSQL database
- getting data from REST API
- providing data to REST API.
In this tutorial, we will create a simple java component with Java Spring Boot scaffolder with a connection to the PostgreSQL database storage. We want to expose a single REST endpoint for getting the basic client data information, creating a microservice CRUD layer above the DB storage.

#
Project sourceThis example project can be cloned from: http://gitlab.factory.codenow-control.codenow.com/public-docs/java-spring-boot-demo/java-spring-boot-rest-server-with-postgresql.git
#
Prerequisites- Prepare your local development environment for CodeNOW with Spring Boot.
- Follow the tutorial instructions in the Java Spring Boot Local Development tutorial.
- Run PostgreSQL locally.
- You can run PostgreSQL directly or using docker compose.
- The configuration file for docker-compose can be downloaded from the link that can be found in the section Docker compose and third-party tools of the Java Spring Boot Local Development tutorial.
- Create a new component
- For details, see the section Prerequisites of the Java Spring Boot Local Development tutorial.
#
StepsOpen your IDE, import the created component and start coding:
Add these maven dependencies to your pom.xml file:
Define the JPA entity Client. This simple table will store the basic client data:
Generate getters and setters with your IDE
Create a new ClientRepository, which is a basic CRUD interface for Spring Boot data DB access:
For more details about Spring Boot @Repository annotation, see: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Repository.html
Create a new controller and put all the parts together
For more details about Spring Boot controllers, see: https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch15s03.html
Next prepare database configuration:
Go to the PgAdmin console (http://localhost:5050 if using compose-postgre from our Local development manual) and create a new db client-db with the scheme client-data.
Add maven dependency to your pom.xml:
Now change the configuration in config/application.yaml:
Fill {db user} and {db password} according to your configuration
Make sure you follow yaml syntax (especially whitespaces)
Try to build and run the application in your IDE. After startup, you should be able to access your new controller’s swagger: http://localhost:8080/swagger/index.html
- For correct setup, check the Readme.md in the project root or see the tutorial Java Spring Boot Local Development, section Prepare local development IDE


#
Deploy to CodeNOWIf your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment.
- Check the Get New PostgreSQL user manual to get CodeNOW managed component properties.
- Make sure to change the application.yaml properties from the local to the production setup.
- For more information about application deployment, see the Application Deployment and Deployment Monitoring tutorials.
#
What’s next?See our other developer guides: