Java Spring Boot Microservices Example - Step by Step Guide - Ge
Java Spring Boot Microservices Example - Step by Step Guide - Ge
Step-by-Step Guide
Step 1: Create a New Spring Boot Project in Spring Initializr
Project: Maven
Language: Java
Packaging: Jar
Java: 17
▲
Spring Boot DevTools
Spring Data JPA
MySQL Driver
Spring Web
Generate the project and run it in IntelliJ IDEA by referring to the above
article.
1. id
2. name
3. email
4. age
Now we are going to fetch Employee Data from Employee Table in our
Spring Boot project. To do it refer to the following steps. Before moving
to IntelliJ IDEA let’s have a look at the complete project structure for
our Microservices.
Step 3: Make Changes in Your application.properties File
spring.datasource.url=jdbc:mysql://localhost:3306/gfgmicros
ervicesdemo
spring.datasource.username=put your username here
spring.datasource.password=put your password here
Go to the src > main > java > entity and create a class Employee and
put the below code. This is our model class.
package com.gfg.employeaap.entity;
import jakarta.persistence.*;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@Column(name = "age")
private String age;
Go to the src > main > java > repository and create an interface
EmployeeRepo and put the below code. This is our repository where we
write code for all the database-related stuff.
package com.gfg.employeaap.repository;
import com.gfg.employeaap.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
Go to the src > main > java > response and create a class
EmployeeResponse and put the below code.
package com.gfg.employeaap.response;
Go to the src > main > java > service and create a class
EmployeeService and put the below code. This is our service class
where we write our business logic.
package com.gfg.employeaap.service;
import com.gfg.employeaap.entity.Employee;
import com.gfg.employeaap.repository.EmployeeRepo;
import com.gfg.employeaap.response.EmployeeResponse;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
@Autowired
private EmployeeRepo employeeRepo;
@Autowired
private ModelMapper mapper;
Go to the src > main > java > controller and create a class
EmployeeController and put the below code. Here we are going to
create an endpoint ” /employees/{id} ” to find an employee using id.
package com.gfg.employeaap.controller;
import com.gfg.employeaap.response.EmployeeResponse;
import com.gfg.employeaap.service.EmployeeService;
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;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employees/{id}")
private ResponseEntity<EmployeeResponse>
getEmployeeDetails(@PathVariable("id") int id) {
EmployeeResponse employee =
employeeService.getEmployeeById(id);
return ResponseEntity.status(HttpStatus.OK).body(employee);
}
Go to the src > main > java > configuration and create a class
EmployeeConfig and put the below code.
package com.gfg.employeaap.configuration;
import com.gfg.employeaap.service.EmployeeService;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmployeeConfig {
@Bean
public EmployeeService employeeBean() {
return new EmployeeService();
}
@Bean
public ModelMapper modelMapperBean() {
return new ModelMapper();
}
<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>
To run your Employee Microservice src > main > java >
EmployeeServiceApplication and click on the Run button. If everything
goes well then you may see the following screen in your console.
Please refer to the below image.
GET: http://localhost:8080/employees/1
{
"id": 1,
"name": "Amiya",
"email": "ar@gmail",
"age": "25"
}
Advertise with us
Communication using FeignClient
with Example
Similar Reads
6 min read
15 min read
Java Spring Boot Microservices - Develop API Gateway Using Spring Cloud…
The API Gateway Pattern in some cases stands for “Backend for frontend†. It is basically the
entry gate for taking entry into any application by an external source. The pattern is going on in a…
4 min read
Java Spring Boot Microservices - Developing Service Discovery
In Microservices, Service Discovery helps us by providing a database of available service instances
so that services can be discovered, registered, and de-registered based on usage. For a detailed…
3 min read
6 min read
Java Spring Boot Microservices – Integration of Eureka and Spring Cloud…
Microservices are small, loosely coupled distributed services. Microservices architecture evolved as
a solution to the scalability, independently deployable, and innovation challenges with Monolithic…
5 min read
4 min read
10 min read
8 min read
3 min read
Corporate & Communications
Address:
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar
Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida,
Gautam Buddh Nagar, Uttar Pradesh,
201305
Advertise with us
Company Languages
About Us Python
Legal Java
Privacy Policy C++
In Media PHP
Contact Us GoLang
Advertise with us SQL
GFG Corporate Solution R Language
Placement Training Program Android Tutorial
GeeksforGeeks Community Tutorials Archive