Complete microservices architecture with real examples — Step 2 — User microservice

This image is recreated by agnasarp.com and used only for information purposes. This does not have any bind with any real-world products or services. Image source: pixabay.com

Step 2 — User Microservice

Initialization details

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

Project Structure

Project structure of user service in IntelliJ Idea

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-user-microservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>agnasarp-user-microservice</name>
<description>Agnasarp User Microservice</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.1</spring-cloud.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>
<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.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: 8380

AgnasarpUserMicroserviceApplication.java

package com.agnasarp.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class AgnasarpUserMicroserviceApplication {

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

@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}

UserController.java

package com.agnasarp.user.controller;

import com.agnasarp.user.entity.User;
import com.agnasarp.user.service.UserService;
import com.agnasarp.user.vo.ResponseTemplateVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;

@PostMapping("/")
public User saveUser(@RequestBody User user) {
log.info("Inside saveUser method of UserController");
return userService.saveUser(user);
}

@GetMapping("/{id}")
public ResponseTemplateVo getUserById(@PathVariable("id") Long userId) {
log.info("Inside getUserById method of UserController");
return userService.getUserWithDepartment(userId);
}
}

UserService.java

package com.agnasarp.user.service;

import com.agnasarp.user.entity.User;
import com.agnasarp.user.repository.UserRepository;
import com.agnasarp.user.vo.Department;
import com.agnasarp.user.vo.ResponseTemplateVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class UserService {
private static final Logger log = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserRepository userRepository;
@Autowired
private RestTemplate restTemplate;

public User saveUser(User user) {
log.info("Inside saveUser method of UserService");
return userRepository.save(user);
}

public ResponseTemplateVo getUserWithDepartment(Long userId) {
log.info("Inside getUserWithDepartment method of UserService");
ResponseTemplateVo responseTemplateVo = new ResponseTemplateVo();
User user = userRepository.getUserByUserId(userId);
Department department = restTemplate.getForObject("http://localhost:8280/departments/" + userId, Department.class);
responseTemplateVo.setUser(user);
responseTemplateVo.setDepartment(department);
return responseTemplateVo;
}

}

UserRepository.java

package com.agnasarp.user.repository;

import com.agnasarp.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User getUserByUserId(Long userId);
}

User.java

package com.agnasarp.user.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
@NoArgsConstructor
@AllArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
private String firstName;
private String lastName;
private Long departmentId;
}

View Objects

Department.java

package com.agnasarp.user.vo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
private Long departmentId;
private String departmentName;
private String departmentAddress;
private String departmentCode;
}

ResponseTemplateVo.java

package com.agnasarp.user.vo;

import com.agnasarp.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseTemplateVo {
private User user;
private Department department;

public void setUser(User user) {
this.user = user;
}

public void setDepartment(Department department) {
this.department = department;
}
}

Service call

cURL

curl --location --request POST 'http://localhost:8380/users/' \
--header 'Content-Type: application/json' \
--data-raw '{
"firstName":"John",
"lastName":"Smith",
"departmentId":"1"
}'

Postman call

Save user scenario

cURL

curl --location --request GET 'http://localhost:8380/users/1'

Postman call

Get user scenario

Download source from github: https://github.com/Agnasarp/agnasarp-user-microservice

Go to Step 1 — 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

Rust Day 12: Leetcode — Balanced Binary Tree

Experiences with .NET Core

The Power of Community

Intro to Reactive Spring — Spring WebFlux

TeraCrawler.io vs. Scrapy: Which is Better for Web Scraping?

TEST DRIVEN DEVELOPMENT(TDD) WITH PHP, PHPUNIT AND LARAVEL.

Functions in Go

How to set up and write Browser based tests for an existing Drupal site/database.

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

A Quick Way to Document and Test Spring Boot RESTful Services with Swagger UI

Mock S3 service for local development

AWS Lambda to invoke other lambda function asynchronously in same region using Java

Spring Microservices in action short stories