How to create a Spring Boot Microservice using Cookiecutter

Learn how to create a Spring Boot microservice using an existing Cookiecutter template by following these simple steps.

Aditya Bansal
October 13, 2021

This article is part of a Cookiecutter series - previously, we posted about standardizing project templates using Cookiecutter. 

Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications that you can just run. You can take this one step further by using a Cookiecutter template to create your Spring Boot microservice. As we mentioned in our previous post, the benefits of using templates to create new services include enforcing standards, ensuring consistency, and accelerating developer velocity.

In this post, we’ll focus on how to use an existing Spring Boot template to create new microservices across your organization. 

Getting started

1. If you don’t already have Cookiecutter, install the CLI on your local setup. Follow the latest installation instructions.

2. Find an existing Spring template or create one from scratch! For the purpose of this article, we’ll use an existing one from Github.

Getting the folder structure ready

1. Create a new folder on your local setup with this command:

mkdir SpringBootLesson && cd SpringBootLesson

2. Clone the template into your newly created folder with this command: 

git clone

Now that the template is cloned, we can go over what we’re looking at, starting with the tree structure of a typical Cookiecutter template!

Let’s quickly break down what we’re seeing in the image above.  You’ll notice a bunch of directories with {{}} in the names – these are “templated” fields that get filled in based on inputs from the user. For example, the directory here called {{ cookiecutter.app_name }} will actually just be the name of the app passed in by the user.

The variables used by the template are defined in a file called cookiecutter.json, which is the central point of any cookiecutter template. Anything that we want to parameterize for the final output would go in this file. This includes the things like:

  • Basic project information like project name and description, which might be used to generate directories, package names, steps in a CI pipeline, or more
  • Configurations that may differ between projects, such as the port a service should run on or what database type it should be configured with.

In our sample cookiecutter.json, we see the following fields:

    “project_name”: “Simple Spring Boot App”,
    “app_name”: “simpleapp”,“server_port”: 8080,
    “_copy_without_render”: [“.gradle”, “gradle/“, “*gradlew”,

Each key in the JSON refers to one of the input parameters for each new service so “app_name” refers to the name of the app. This can be anything you like and whatever you pick will be used as the folder name that is referred to as {{ cookiecutter.app_name }} in the image above.

Creating the Spring Boot microservice

Now that the folder structure is ready, let’s create our first Spring microservice!

1. From the SpringBootLesson folder, run cookiecutter cookiecutter-spring-boot 

You should see a prompt that looks like this: 

This is Cookiecutter asking us to fill out the details of our new app.

2. Fill out the prompts:
project_name: First Project
app_name: first_project

Press enter and you’ll see a new folder with whatever app_name you chose for your app! It’s that simple :) 

3. cd into the newly created folder, and run

./gradlew bootRun

Within a few seconds, you should see a message saying Started Application in 2.09 seconds.

4. Voila! You just created your first Spring app using Cookiecutter!

Enhancing the template

Now that we feel comfortable creating services from scratch, we can get creative! Let’s make the port our service runs on configurable.

1. In the template, find the application.yml file. It should be located under the src/main/resources directory

2. Change the second line from port: 8080 to port: {{cookiecutter.server_port}}

3. Change the root cookiecutter.json to include the server_port variable:

    "project_name": "Simple Spring Boot App",
    "app_name": "simpleapp","server_port": 8080,
    "server_port": 8080,
    "_copy_without_render": [".gradle", "gradle/", "*gradlew", "*gradlew.bat"]

4. From the SpringBootLesson directory, run:

cookiecutter cookiecutter-spring-boot

5. Enter 8081 when asked for the server_port.

6. Voila! This time, when you run ./gradlew bootRun from the newly generated service, you’d see the server running on port 8081!

The corresponding code for this blog post can be found here!

Some other things you add to your Spring Cookiecutter template are: 

  • Logging levels for the app
  • Your package name
  • Datasource configurations (max pool size, connection pool limits etc.)
  • Environment variables used throughout the app

As you can see, Cookiecutter can standardize service creation by moving all the configurations & metadata to a template file. This ensures that structure is the same across microservices and that your teams can create microservices that match your standards quickly and easily.

Still think it’s a pain to create new services? Book a demo with us to see our service creation product offering. To keep learning about service creation using Cookiecutter, subscribe to our blog.

Aditya Bansal
What's driving urgency for IDPs?