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

Microservices Architecture

Step 1 — Department Microservice

Initialization details

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

Project structure

pom.xml

<?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>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.agnasarp</groupId>
<artifactId>agnasarp-department-microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>agnasarp-department-microservice</name>
<description>Agnasarp Department Microservice</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>

application.yml

server:
port: 8280

AgnasarpDepartmentMicroserviceApplication.java

package com.agnasarp.department;

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

@SpringBootApplication
public class AgnasarpDepartmentMicroserviceApplication {

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

}

DepartmentController.java

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.*;

@RestController
@RequestMapping("/departments")

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

@PostMapping("/")
public Department saveDepartment(@RequestBody Department department) {
log.info("Inside saveDepartment method of DepartmentController");
return departmentService.saveDepartment(department);
}

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

DepartmentService.java

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;

@Service
public class DepartmentService {

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

public Department saveDepartment(Department department) {
log.info("Inside saveDepartment method of DepartmentService");
return departmentRepository.save(department);
}

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

DepartmentRepository.java

package com.agnasarp.department.repository;

import com.agnasarp.department.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

Department.java

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;

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long departmentId;
private String departmentName;
private String departmentAddress;
private String departmentCode;
}

Service call

cURL

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

cURL

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

Postman call

Download source from github:

Download department-service: https://github.com/Agnasarp/agnasarp-department-microservice

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Render Token and the Future of Media

Liquid left navigation using SVG animate

How to make a Rock Paper Scissor Alexa Skill game in Python??

What is the syllabus of the DXC technology placement test?

Java 8: Sort Map By Value In Descending Order

CyberKongz VX Art Contest

Roadmap to success

[Week 2, August 2021] Fleta Weekly Report

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

Agnasarp

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

More from Medium

Example of Spring Cloud Function with AWS Lambda

AWS SERVERLESS ARCHITECTURE IN JAVA

3 Useful Tips for Developers when using Spring Cloud Config

Proxy Design Pattern Usage