Featured Article

Java Spring Boot Microservice Example: Build Scalable APIs

Kenneth Jul 13, 2026

Spring Boot, a paradigm shift in Java application development, simplifies the process by providing a platform that eases the creation of standalone, production-ready spring-based applications. When it comes to building microservices, Spring Boot's easiness and efficiency make it a natural fit. Let's dive into an example to understand how.

How to Build Event-Driven Microservices with Spring Boot and Kafka? Example Tutorial
How to Build Event-Driven Microservices with Spring Boot and Kafka? Example Tutorial

Firstly, we'll need to set up a new project using the Spring Initializr tool. Choose "Web" as the dependency, and ensure "Microservice" is selected under the "Project Metadata" section. This will give us a basic microservice with the necessary dependencies to start building.

Java + Spring Boot + Microservices Example - Step by Step Guide
Java + Spring Boot + Microservices Example - Step by Step Guide

Project Structure

Once generated, our project should have a structure similar to this:

8 Best Spring Boot Courses for Java Developers in 2026
8 Best Spring Boot Courses for Java Developers in 2026

 src
 ├─ main
 │  ├─ java
 │  │  └─ com.example.demo
 │  │     └─ DemoApplication.java
 │  └─ resources
 │     └─ application.properties
 ├─ test
 │  └─ java
 │     └─ com.example.demo
 │        └─ DemoApplicationTests.java
 └─ pom.xml

Application Class

Microservices In Java
Microservices In Java

The `DemoApplication` class is the entry point of our microservice. It's annotated with `@SpringBootApplication`, which marks it and its sub-packages as candidates for component scanning.

Our microservice is now ready to start. Let's add a simple controller to handle requests.

Adding a Controller

Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot Project Ideas for Resume | Real-World Examples

We'll create a new class `GreetingController` in the `com.example.demo` package:

@RestController
public class GreetingController {

    @GetMapping("/greet")
    public Greeting greet(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(String.format("Hello, %s!", name));
    }
}

Dockerizing the Microservice

an image of a computer screen with text and numbers on it, as well as other items
an image of a computer screen with text and numbers on it, as well as other items

Dockerizing our application ensures it runs consistently across different environments. Let's add a Dockerfile to our project:

# Base image
FROM openjdk:8-jdk-alpine

# Set environment variables
ENV SPRING_PROFILES_ACTIVE docker

# Set working directory
WORKDIR /app

# Copy the assembled application to the container
COPY target/*.jar app.jar

# Execute the assembled application
ENTRYPOINT ["java","-jar","/app/app.jar"]

Complete Spring Framework Cheat Sheet | Spring Boot, MVC
Complete Spring Framework Cheat Sheet | Spring Boot, MVC
☕ Build a Simple Web App with Spring Boot in Java
☕ Build a Simple Web App with Spring Boot in Java
6 Best Microservices Courses for Java and Spring Boot Developers in 2024
6 Best Microservices Courses for Java and Spring Boot Developers in 2024
Spring Boot MVC REST Annotations With Examples - JavaTechOnline
Spring Boot MVC REST Annotations With Examples - JavaTechOnline
Spring vs Spring Boot – Key Differences Every Java Developer Should Know
Spring vs Spring Boot – Key Differences Every Java Developer Should Know
Spring Boot Application Properties Example - Java Code Geeks
Spring Boot Application Properties Example - Java Code Geeks
Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn
Spring Boot Project Structure for Clean & Scalable Code | Raghvendra Pratap posted on the topic | LinkedIn
Java Spring Boot Development Services in USA
Java Spring Boot Development Services in USA
Spring Boot Clean Architecture 📁🚀
Spring Boot Clean Architecture 📁🚀

With that, we've successfully created and containerized our first Spring Boot microservice. Next steps could include using a cloud gateway like Spring Cloud Gateway to route requests to our microservice, and employing services like Spring Cloud Config and Spring Cloud Eureka for configuration management and service discovery.

Embarking on this journey with Spring Boot and microservices opens up a world of possibilities in scalable, maintainable, and efficient application development. Explore, experiment, and enjoy the process!