Skip to content

c. Service

The implementation of the Account microservice is defined in the account-service module, which contains the implementation of the API defined in the account module, as well as, the database access and the business logic for the Account microservice.

classDiagram
    namespace account {
        class AccountController {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountIn {
            -String name
            -String email
            -String password
        }
        class AccountOut {
            -String id
            -String name
            -String email
        }
    }
    namespace account-service {
        class AccountResource {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountService {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class AccountRepository {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
        class Account {
            -String id
            -String name
            -String email
            -String password
            -String sha256
        }
        class AccountModel {
            +create(AccountIn accountIn): AccountOut
            +delete(String id): void
            +findAll(): List<AccountOut>
            +findById(String id): AccountOut
        }
    }
    <<Interface>> AccountController
    AccountController ..> AccountIn
    AccountController ..> AccountOut

    <<Interface>> AccountRepository
    AccountController <|-- AccountResource
    AccountResource *-- AccountService
    AccountService *-- AccountRepository
    AccountService ..> Account
    AccountService ..> AccountModel
    AccountRepository ..> AccountModel

1. Repository

Create the repository for the Account interface on GitHub, and clone it as a submodule to your local machine;

git submodule add <repository_url> api/account-service
📁 api/
├── 📁 account/
└── 📁 account-service/

2. Code

Now, we will code the implementation of the Account microservice, which consists of a lot of classes. The resulting directory structure will look like this:

📁 api/
└── 📁 account-service/
    ├── 📁 src/
       └── 📁 main/
           ├── 📁 java/
              └── 📁 store/
                  └── 📁 account/
                      ├──  AccountApplication.java
                      └──  AccountResource.java
           └── 📁 resources/
               └──  application.yaml
    └──  pom.xml
Class Description
AccountResource This class is responsible for the API endpoints of the Account microservice, implementing the AccountController interface defined in the account module, and using the AccountService to handle the business logic of the API endpoints.
AccountApplication This class is the main class of the Account microservice, which is responsible for running the Spring Boot application. It contains the main method, which is the entry point of the application.

To code this microservice, we will use the Spring Boot framework, through the Spring Initializr, at [https://start.spring.io/], which is a web-based tool that allows us to generate a Spring Boot project with the necessary dependencies and configurations.

Spring Initializr
Spring Initializr

Note:

  • Project: Maven
  • Language: Java
  • Spring Boot: 4.0.3 (stable version for now)
  • Group: store (the company name, for example)
  • Artifact: account-service (the implementation name)
  • Package name: store.account
  • Packaging: Jar
  • Configurarion: YAML
  • Java: 25 (LTS version for now)

Additionally, we need to add the following dependencies:

  • Spring Web: a dependency that allows us to create RESTful web services using the Spring framework. It provides the necessary tools and libraries to handle HTTP requests and responses, as well as, to define API endpoints and controllers.

  • Lombok: a Java library that helps to reduce boilerplate code by generating getters, setters, constructors, and other common methods at compile time using annotations.

  • OpenFeign: a declarative web service client that simplifies the process of making HTTP requests to other microservices. It allows us to define interfaces for our API clients and automatically generates the implementation at runtime.

For now, we will just create the AccountResource and AccountApplication classes, and the application.yaml configuration file. We will implement the business logic and the data persistence in the next sections.

At AccountResource.java, the only endpoint implemented for now is health-check, which is a simple endpoint that returns a 200 OK status code, indicating that the microservice is up and running.

Source

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>store</groupId>
    <artifactId>account-service</artifactId>
    <version>1.0.0</version>
    <name>account-service</name>

    <properties>
        <java.version>25</java.version>
        <spring-cloud.version>2025.1.0</spring-cloud.version>
        <maven.compiler.proc>full</maven.compiler.proc>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>store</groupId>
            <artifactId>account</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
1
2
3
4
5
6
server:
  port: 8080

spring:
  application:
    name: account
package store.account;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AccountApplication {

    public static void main(String[] args) {
        SpringApplication.run(AccountApplication.class, args);
    }

}
package store.account;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AccountResource implements AccountController {

    @Override
    public ResponseEntity<Void> create(AccountIn in) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ResponseEntity<Void> delete(String id) {
        // TODO Auto-generated method stub
        return ResponseEntity.noContent().build();
    }

    @Override
    public ResponseEntity<Void> healthCheck() {
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<List<AccountOut>> findAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ResponseEntity<AccountOut> findById(String id) {
        // just an example
        AccountOut out = AccountOut.builder()
            .name("John")
            .email("JAlbert@xpto.gov")
            .id("1345")
            .build();
        return ResponseEntity.ok(
            out
        );
    }

}

3. Package and Run

mvn clean package
java -jar target/account-service-1.0.0.jar

or

mvn clean package spring-boot:run

Done! The Account Microservice is now ready to be used in the project.

Let's move on to the next section, where we will containerize the Account microservice using Docker, and run it using Docker Compose.

Containerization