Skip to content

A template project for Spring Boot applications.

Notifications You must be signed in to change notification settings

andrecaiado/spring-boot-template

Repository files navigation

Spring Boot Template project

This is a template project for Spring Boot applications.

Contents

Features

  • Exposes an API to perform CRUD operations on a single entity
  • Persists the data in a PostgreSQL database
  • Uses Spring Data JPA for database operations
  • Uses Flyway for database migrations
  • Uses Spring Boot Docker Compose to start and stop a Docker container running the PostgreSQL database
  • Includes a datasource configuration for testing purposes that uses the H2 in-memory database

Requirements

  • Java 17
  • Docker
  • Docker Compose

Getting Started

This section provides a step-by-step guide on how to run the project.

Installation

  1. Clone the repository by executing the following command:
git clone git@github.com:andrecaiado/spring-boot-template.git
  1. Navigate into the project directory:
cd your-repository-name
  1. Install the dependencies by executing the following command:
./mvnw clean install
  1. Run the application by executing the following command:
./mvnw spring-boot:run

Try it out with the postman collection

The Postman collection is available here: spring-boot-template-rest-api.postman_collection.json

PostgreSQL database

The PostrgreSQL configuration is located in src/main/resources/application.yaml.

The datasource configuration is located in the docker-compose.yml file so it can be picked up by Spring Boot Docker Compose.

Database migrations with Flyway

Flyway configuration is located in src/main/resources/application.yaml.

The migration files are located at src/main/resources/db/migration.

Spring Boot Docker Compose

With Spring Boot Docker Compose, the container running the PostgreSQL database will be automatically started when the application is started and stopped when the application is stopped.

Spring Boot Docker Compose will detect and use the docker-compose.yml file located in the root of the project.

The data source will be configured with the properties defined in the docker-compose.yml file.

In-memory database for testing

The datasource configuration is located in src/test/resources/application.yaml.

When a test loads the application context or explicitly calls this configuration, the H2 in-memory database will be initialized.

The migrations will be applied from the files located at src/main/resources/db/migration. To apply a different migration to the test database, create the migration files in the scr/test/resources and specify the configuration in the scr/test/resources/application.yaml file.

spring:
  application:
      flyway:
        locations: classpath:/db/migration
        schemas: employees
        baselineOnMigrate: true
        enabled: true