Creating a Spring boot microservice template

Mikhail Aleksseev
3 min readMar 13, 2022

Microservices architecture has many advantages over monolithic architecture. However, in the process of system development we have to create a lot of microservices. To make development process faster and do not create them from scratch every time, in this article we will create a template of microservice. Will be shown process of development in a total, with out go into detail in technology’s. There are a lot of instruction by each of them.

To use the template, you will just need to rename the microservice and fill it with the necessary business logic.

Project code you can view in my GitHub https://github.com/mihailaleksseev/SpringDemoService

For creation microservice we will use Java 17, Spring Boot, Gradle, PostgreSQL, Liquibase and Junit.

Create a new project at https://start.spring.io/ with the necessary dependencies.

Download the created project and open it in IDEA.
Specify SDK in the project settings.

Add dependencies that were not on the start.spring.io page in build.gradle.

To development become much more comfortable create docker-compose file and add to them postgres. For start up you should run docker-compose up command in the docker-compose folder.

Liquibase and postgres configuration

Let’s specify the database connection settings and liquibase migration files in application.yaml

Create the necessary liquibase package structure and add a test migration

Let’s run the project and check in the database that the migration was run successfully.

Swagger configuration and application business logic

OpenApi is plugged into build.gradle with the command.

implementation ‘org.springdoc:springdoc-openapi-ui:1.6.4’

For the swagger documentation to be complete, you need to do two things:

1. Create DTO fields swagger description.

2. Create controller methods swagger description.

OpenAPI documentation result view

Tests

It’s hard to imagine modern project without tests. After the basic functionality is implemented, write tests. Let’s create a file structure for tests.

Let’s configure H2 database and write a test data migration for demo table.

Write test for all methods from DemoController. In this example we checking only positive results. In order for the tests to be more reliable, it is necessary to add a negative scenario check.

Let’s check that the tests work.

Thus we did a spring microservice template, which will help as to save time.

In the future it will be great add message broker such as Rabbit or Kafka, cache managed and other interesting and useful things.

--

--