Microservices Communication via HTTP and JSON
Last Updated :
20 May, 2024
A microservice architecture is used to build an application as a collection of loosely coupled, independently deployable services. These applications can interact with each other to achieve synergistic functionality. The most common way for microservices to communicate is to use JSON as the data exchange format using HTTP which is a widely supported protocol. It can provide a reliable way for any service to send a request and receive a response. JSON is a small data exchange format and is easy to parse and manipulate.
Microservices communicate via HTTP by calling each other’s APIs. Each microservice can expose a RESTful API and other microservices can interact with these APIs to perform various tasks. JSON can be used as a data exchange format because it is lightweight and easy to work with.
Overview of how microservices communicate
- Service discovery: Microservices can register with a user discovery tool like Eureka so they can find each other.
- API Gateway: This can act as an entry point for clients, sending requests to the appropriate microservices.
- HTTP requests: Microservices can send HTTP requests to communicate with each other via JSON to exchange data.
Implementation of the Microservices Communication via HTTP and JSON
We will develop the two simple microservices. Then two services will communicate through the HTTP and JSON of the application.
Create the User-Service
Step 1: Create a new Spring Boot project using Spring Initializr, and add the below dependencies:
- Spring Web
- Apache ActiveMQ
- Spring DevTools
- Lombok
After this project creation done, the project structure will be like below:

Step 2: Open application.properties file and add the application name, server port configuration of the project.
spring.application.name=user-service
server.port=8082
Step 3: Create the User class
Go to src > org.example.userservice > model > User and put the below code.
Java
package org.example.userservice.model;
import lombok.Data;
@Data
public class User {
private String userId;
private String name;
private String email;
public User() {
}
public User(String userId, String name, String email) {
this.userId = userId;
this.name = name;
this.email = email;
}
}
Step 4: Create the UserService class.
Go to src > org.example.userservice > service > UserService and put the below code.
Java
package org.example.userservice.service;
import org.example.userservice.model.User;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public User getUserById(String userId) {
return new User(userId, "John Doe", "[email protected]");
}
public User getUserByName(String name) {
// Mock data, in real scenarios, fetch from database
if ("John Doe".equals(name)) {
return new User("U12345", name, "[email protected]");
}
return null;
}
}
Step 5: Create the UserController Class
Go to src > org.example.userservice > controller > UserController and put the below code.
Java
package org.example.userservice.controller;
import org.example.userservice.model.User;
import org.example.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{userId}")
public ResponseEntity<User> getUser(@PathVariable String userId) {
User user = userService.getUserById(userId);
return ResponseEntity.ok(user);
}
@GetMapping("/byname")
public ResponseEntity<User> getUserByName(@RequestParam String name) {
User user = userService.getUserByName(name);
return ResponseEntity.ok(user);
}
}
Step 6: Open the main class. (No changes are required)
Java
package org.example.userservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
pom.xml:
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>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-service</name>
<description>user-service</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
Step 7: Run the application
Now we will run the project and it will run at port 8082.

Create the Order-service
Step 1: Create a new Spring Boot project using Spring Initializr, and add below dependencies:
- Spring Web
- Apache ActiveMQ
- Spring DevTools
- Lombok
After the Project creation done, the folder structure will be like below image.

Step 2: Open application.properties file and add the application name, server port configuration.
spring.application.name=order-service
server.port=8081
Step 3: Create the Order class
Go to src > org.example.orderservice > model > Order and put the below code.
Java
package org.example.orderservice.model;
import lombok.Data;
@Data
public class Order {
private String orderId;
private String userId;
private double amount;
public Order() {
}
public Order(String orderId, String userId, double amount) {
this.orderId = orderId;
this.userId = userId;
this.amount = amount;
}
}
Step 4: Create the OrderDetails class
Go to src > org.example.orderservice > model > OrderDetails and put the below code.
Java
package org.example.orderservice.model;
import lombok.Data;
@Data
public class OrderDetails {
private Order order;
private User user;
public OrderDetails() {
}
public OrderDetails(Order order, User user) {
this.order = order;
this.user = user;
}
}
Step 5: Create the User class
Go to src > org.example.orderservice > model > User and put the below code.
Java
package org.example.orderservice.model;
import lombok.Data;
@Data
public class User {
private String userId;
private String name;
private String email;
public User() {
}
public User(String userId, String name, String email) {
this.userId = userId;
this.name = name;
this.email = email;
}
}
Step 6: Create the RestTemplateConfig class
Go to src > org.example.orderservice > config > RestTemplateConfig and put the below code.
Java
package org.example.orderservice.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Step 7: Create the OrderService class
Go to src > org.example.orderservice > service > OrderService and put the below code.
Java
package org.example.orderservice.service;
import org.example.orderservice.model.Order;
import org.example.orderservice.model.OrderDetails;
import org.example.orderservice.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.ResourceAccessException;
@Service
public class OrderService {
@Autowired
private RestTemplate restTemplate;
public OrderDetails getOrderDetails(String orderId) {
// Mock data
Order order = new Order(orderId, "U12345", 250.75);
User user = restTemplate.getForObject("http://localhost:8082/users/" + order.getUserId(), User.class);
return new OrderDetails(order, user);
}
public OrderDetails getOrderDetailsByUserName(String name) {
try {
User user = restTemplate.getForObject("http://localhost:8082/users/byname?name=" + name, User.class);
if (user != null) {
// Mock data
Order order = new Order("12345", user.getUserId(), 250.75);
return new OrderDetails(order, user);
} else {
return null;
}
} catch (ResourceAccessException e) {
System.err.println("Error connecting to UserService: " + e.getMessage());
return null;
}
}
}
Step 8: Create the OrderController class
Go to src > org.example.orderservice > controller > OrderController and put the below code.
Java
package org.example.orderservice.controller;
import org.example.orderservice.model.OrderDetails;
import org.example.orderservice.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{orderId}")
public ResponseEntity<OrderDetails> getOrder(@PathVariable String orderId) {
OrderDetails orderDetails = orderService.getOrderDetails(orderId);
return ResponseEntity.ok(orderDetails);
}
@GetMapping("/byusername")
public ResponseEntity<OrderDetails> getOrderByUserName(@RequestParam String name) {
OrderDetails orderDetails = orderService.getOrderDetailsByUserName(name);
return ResponseEntity.ok(orderDetails);
}
}
Step 9: Open the main class and write the below code. (No changes are required)
Java
package org.example.orderservice;
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 OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
pom.xml:
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>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order-service</name>
<description>order-service</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
Step 10: Run the application
Once we run the application, the application will start at port 8081.

Order by username Endpoint Testing:
GET http://localhost:8081/order/byusername?name=John Doe
Output:

By following these steps, we can set up the two microservices (UserService, OrderService ) that communicate via the HTTP using JSON. This example project demonstrates how microservices can interact seamlessly and it allow us to build scalable and maintainable applications.
Similar Reads
Inter-Service Communication in Microservices
Inter-Service Communication in Microservices explains how different services within a microservices architecture communicate with each other. In microservices, applications are broken down into smaller, independent services that work together. These services need to exchange data to function properl
10 min read
Microservices Communication Patterns
Microservices Communication Patterns explore how small, independent services in a software system talk to each other. These patterns are crucial for ensuring that microservices work together smoothly and efficiently. They cover methods like synchronous and asynchronous messaging, using APIs, message
11 min read
Microservices Communication with NATS
Microservices architectures enable the development of scalable and independent services within the system, promoting agility and robustness. Communication between these services is crucial for the overall performance and functionality of the application. NATS is a high-performance messaging system t
6 min read
API Composition Pattern in Microservices
In today's world of software development, microservices have become a popular way to build applications. One effective approach within this architecture is the API Composition Pattern. This pattern allows developers to combine multiple microservices into a single, unified API response. By doing so,
14 min read
Microservices Communication with Apache Kafka in Spring Boot
Apache Kafka is a distributed streaming platform and can be widely used to create real-time data pipelines and streaming applications. It can publish and subscribe to records in progress, save these records in an error-free manner, and handle floating records as they arrive. Combined with Spring Boo
6 min read
How to Communicate with Backend Services using HTTP in Angular?
To communicate with backend services using HTTP in Angular, you typically use the HttpClient module, which is part of the @angular/common/http package. This module allows you to interact with RESTful APIs, fetch data, submit forms, perform CRUD operations, and more. PrerequisitesMongoDBExpressJSAngu
7 min read
HTTP vs gRPC for Microservices
As organizations increasingly adopt microservices architecture, the choice of communication protocol becomes crucial for ensuring performance, scalability, and maintainability. This article explores the differences between HTTP and gRPC, two popular protocols, to help you make an informed decision.
4 min read
How Eureka Server and Client Communicate with Each Other in Microservices?
Service Discovery is one of the major things of a microservice-based architecture. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly functional, with each server copying the state of the registered services to the others. In the previou
5 min read
Devops and Microservices
DevOps and microservices have transformed how organizations develop and deploy software. DevOps fosters collaboration between development and operations teams, enhancing efficiency and innovation. Microservices architecture, on the other hand, enables modular development, allowing teams to build, de
7 min read
Spring Boot Microservices Communication using FeignClient with Example
FeignClient also known as Spring Cloud OpenFeign is a Declarative REST Client in Spring Boot Web Application. But what do you mean by Declarative REST Client? It means we need to specify the client specification as an Interface and Spring Boot will take care of the implementation for us. Writing web
15 min read