Complete microservices architecture with real examples — Step 1 — Department microservice

Hello everyone! Today we are going to show you how you can create a microservice as a part of a complete microservices architecture that we will implement step by step in future posts. You can see the full architecture in the below diagram.

Microservices Architecture

As the first step, we will implement Department Service in this post. You can find all details step by step of Department Service with all source code.

Step 1 — Department Microservice

Here we will quickly go to the technology stack that we are going to use in our implementation.

Initialization details

Spring Initialzr — Department Service
  • Project build tool: Maven
  • Language: Java
  • Spring boot: 2.4.2
  • Project Metadata

Group: com.agnasarp

Artifact: agnasarp-department-microservice

Name: agnasarp-department-microservice

Description: Agnasarp Department Microservice

Package name: com.agnasarp.department

Packaging: Jar

Java version: 8

  • Dependencies:

Spring Web: Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.

Spring Data JPA: Persist data in SQL stores with Java Persistence API using Spring Data and Hibernate.

H2 Database: Provides a fast in-memory database that supports JDBC API and R2DBC access, with a small (2mb) footprint. Supports embedded and server modes as well as a browser based console application.

Lomok: Java annotation library which helps to reduce boilerplate code.

Then, we will import the project into IntelliJ Idea.

Project structure


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
<relativePath/> <!-- lookup parent from repository -->
<description>Agnasarp Department Microservice</description>


port: 8280

package com.agnasarp.department;

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

public class AgnasarpDepartmentMicroserviceApplication {

public static void main(String[] args) {, args);


package com.agnasarp.department.controller;

import com.agnasarp.department.entity.Department;
import com.agnasarp.department.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


public class DepartmentController {
private static final Logger log = LoggerFactory.getLogger(DepartmentController.class);
private DepartmentService departmentService;

public Department saveDepartment(@RequestBody Department department) {"Inside saveDepartment method of DepartmentController");
return departmentService.saveDepartment(department);

public Department getDepartmentById(@PathVariable("id") Long departmentId) {"Inside getDepartmentById method of DepartmentController");
return departmentService.getDepartmentById(departmentId);

package com.agnasarp.department.service;

import com.agnasarp.department.entity.Department;
import com.agnasarp.department.repository.DepartmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class DepartmentService {

private static final Logger log = LoggerFactory.getLogger(DepartmentService.class);
private DepartmentRepository departmentRepository;

public Department saveDepartment(Department department) {"Inside saveDepartment method of DepartmentService");

public Department getDepartmentById(Long departmentId) {"Inside getDepartmentById method of DepartmentService");
return departmentRepository.getDepartmentByDepartmentId(departmentId);

package com.agnasarp.department.repository;

import com.agnasarp.department.entity.Department;
import org.springframework.stereotype.Repository;

public interface DepartmentRepository extends JpaRepository<Department, Long> {
Department getDepartmentByDepartmentId(Long departmentId);

package com.agnasarp.department.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Department {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
private String departmentName;
private String departmentAddress;
private String departmentCode;

Service call

Create department scenario


curl --location --request POST 'http://localhost:8280/departments/' \ --header 'Content-Type: application/json' \ --data-raw '{ "departmentName":"IT", "departmentAddress":"Colombo 07, Srilanka", "departmentCode":"IT-001" }'

Postman call

Get department scenario


curl --location --request GET 'http://localhost:8280/departments/1' \ --data-raw ''

Postman call

Download source from github:

Download department-service:

Originally published at on February 17, 2021.



Agnasarp is a technology-focused blog that has enough information about cutting-edge technologies that you can use for your problems. Stay with us!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store

Agnasarp is a technology-focused blog that has enough information about cutting-edge technologies that you can use for your problems. Stay with us!