Spring Boot with Java MicroServices

Example 2: Java Spring Boot with Micro Services using REST Call

Let we have two services like address service and employee service. In employee service we call to address service then through employee service we will get employee details and address details. In this article we will see based on employee id we will get employee and address details.

Here every services are spring boot project.

Project Structure of Address-Service and Emp-Service

img img

Here in this project, first develop Address-Service application


Let’ create an address table and employee table and insert some records in MySQL environment.

img img
img
img

Here employee_id column of address table is representing as foreign key, that means the id column of employee table and employee_id column of address table is common.


Let’s develop Address-Service application for fetching data from address table.

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>3.1.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.silan.address</groupId>
	<artifactId>Address-Service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Address-Service</name>
	<description>Address Service</description>
	<properties>
		<java.version>17</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>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
    		<groupId>org.modelmapper</groupId>
    		<artifactId>modelmapper</artifactId>
    		<version>3.1.0</version>
		</dependency>
		
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/priyanka
spring.datasource.username=root
spring.datasource.password=Silan@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.ddl-auto=update
spring.application.name=address-app
server.port=8081
server.servlet.context-path=/address-app/api

AddressServiceApplication.java
package com.silan.address;

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

@SpringBootApplication
public class AddressServiceApplication {

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

}

Address.java
package com.silan.address.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
public class Address {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	
	private int id;
	private String city;
	private String state;
	private String country;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
}

AddressRepo.java
package com.silan.address.repo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.silan.address.entity.Address;

public interface AddressRepo extends JpaRepository{
	
	
	
	@Query(nativeQuery=true,value="select ea.id, ea.city, ea.state, ea.country from priyanka.address ea join priyanka.employee e on e.id=ea.employee_id where ea.employee_id=:employeeId")
	
	Address findAdressByEmployeeId(@Param("employeeId")int employeeId);

}

AddressResponse.java
package com.silan.address.response;


public class AddressResponse {
	
	private int id;
	private String city;
	private String state;
	private String country;
	
	public int getId() {
		return id;
	}
	public void setId(int aid) {
		this.id = id;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	
	
}

AddressService.java
package com.silan.address.service;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.silan.address.entity.Address;
import com.silan.address.repo.AddressRepo;
import com.silan.address.response.AddressResponse;

@Service
public class AddressService {
	
	
	@Autowired
	private AddressRepo addressRepo;
	
	@Autowired
	private ModelMapper modelMapper;
	
	public AddressResponse findAddressByEmployeeId(int employeeId)
	{
		Address address=addressRepo.findAdressByEmployeeId(employeeId);
		
		AddressResponse addressResponse=modelMapper.map(address, AddressResponse.class);
		
		return addressResponse;
	}
	

}

AddressController.java
package com.silan.address.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.silan.address.response.AddressResponse;
import com.silan.address.service.AddressService;

@RestController
public class AddressController {
	
	@Autowired
	private AddressService addressService;
	
	@GetMapping("/address/{employeeId}")
	public ResponseEntity getAddressByEmployeeId(@PathVariable("employeeId")int id)
	{
		AddressResponse addressResponse=null;
		
		addressResponse=addressService.findAddressByEmployeeId(id);
		
		return ResponseEntity.status(HttpStatus.OK).body(addressResponse);
	}

}

AddressConfig.java
package com.silan.address.config;

import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AddressConfig {
	
	@Bean
	public ModelMapper modelMapper()
	{
		return new ModelMapper();
	}

}

Then we will run the project as Run As->Spring Boot App

img

Now we will open postman and test for fetching data from address table by proper putting endpoint with url.


img
Now we will develop Emp-Service:

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>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.silan</groupId>
<artifactId>Emp_Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Emp_Service</name>
<description>Employee Service</description>
<properties>
<java.version>17</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/empdb
spring.datasource.username=root
spring.datasource.password=Silan@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

spring.jpa.hibernate.ddl-auto=update

spring.application.name=employee-app
server.port=8080
server.servlet.context-path=/employee-app/api

EmpServiceApplication.java
package com.silan;

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

@SpringBootApplication
public class EmpServiceApplication {

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

}

Employee.java
package com.silan.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
public class Employee {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String name;
	private String email;
	private String designation;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	
}

EmployeeRepository.java
package com.silan.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.silan.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee,
Integer>{

}

EmployeeResponse.java
package com.silan.response;


public class EmployeeResponse {
	
	private int id;
	private String name;
	private String email;
	private String designation;
	private AddressResponse addressResponse;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public AddressResponse getAddressResponse() {
		return addressResponse;
	}
	public void setAddressResponse(AddressResponse addressResponse) {
		this.addressResponse = addressResponse;
	}
	

}

EmployeeService.java
package com.silan.service;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.silan.entity.Employee;
import com.silan.repository.EmployeeRepository;
import com.silan.response.EmployeeResponse;
import com.silan.response.AddressResponse;


@Service
public class EmployeeService {
	
	@Autowired
	private EmployeeRepository employeeRepository;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private RestTemplate restTemplate;
	
	public EmployeeResponse getEmployeeById(int id)
	{
		
		Employee employee=employeeRepository.findById(id).get();
		
		
		
		EmployeeResponse employeeResponse=modelMapper.map(employee, EmployeeResponse.class);
		
		AddressResponse addressResponse=restTemplate.getForObject("http://localhost:8081/address-app/api/address/{id}",AddressResponse.class, id);
		
		employeeResponse.setAddressResponse(addressResponse);
		
		return employeeResponse;
	}
	
	

}

EmployeeController.java
package com.silan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.silan.entity.Employee;
import com.silan.response.EmployeeResponse;
import com.silan.service.EmployeeService;

@RestController
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeService;
	
	
	@GetMapping("/employees/{id}")
	ResponseEntity getEmployeeDetails(@PathVariable("id") int id)
	
	{
		EmployeeResponse employeeResponse=employeeService.getEmployeeById(id);
		
		return ResponseEntity.status(HttpStatus.OK).body(employeeResponse);
	}
	
}

Now we will run this Emp-Service app by RunAs->Spring Boot App

img

Then open postman and test for fetching data from employee table.

img

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 Previous