Part 0.
Prerequisites
Before getting start make sure that you have these installed and ready:
1. IDE or text editor ( Eclipse , Intellij )
2. MySQl
3. MongoDB
4. POSTMAN
5. JDK 17 or higher
6. Spring boot
Part 1: Inventory Microservice
1. Initialise the project
Go to https://start.spring.io/ or use “Spring starter project”. You will need Lombok, Spring Data
JPA, Spring Web and MySQL Driver as dependencies. you can specify your own Groud Id, Artifact
Id, Project Name, and Java Version as you wish
✓ Spring Web — To include Spring MVC and embedded Tomcat into your project
✓ Spring Data JPA — Java Persistence API and Hibernate
✓ MySQL Driver — JDBC Driver
✓ Lombok — Java library tool that generates code for minimizing boilerplate code. The library
replaces boilerplate code with easy-to-use annotations
2. Connect Spring Boot Application With the database
Under the resources folder, you can see a file called application.properties. Inside the
application.properties file add those details. We use MySQL as the database therefore we have to specify
our Server port, database name, username, and password (Check your Sql profile for these
information)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:port/databasename
spring.datasource.username = username
spring.datasource.password = password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
server.port=port number
For example:
3. Creating a Inventory Entity (Domain Model )
First of all, you will create a package for models called “entity” package, then create a class called
Inventory.java.
Here you don’t have to generate constructors, getters, and setters because those are done
by @Data, @AllArgsConstructor, and @NoArgsConstructor annotations which were provided
by Lombok dependency.
The @Entity annotation specifies that the class is an entity and is mapped to a database table.
The @Table annotation specifies the name of the database table to be used for mapping.
The @Id annotation specifies the primary key of an entity and @GeneratedValue provides for the
specification of generation strategies for the values of primary keys.
4. Creating Repository Classes (Persistence Layer )
Our next step is creating a repository class. @Repository is a Spring annotation that indicates that the
decorated class is a repository. A repository is a mechanism for encapsulating storage, retrieval, and
search behavior that emulates a collection of objects.
First of all, you will create a package for repository classes called “repository”. Inside the package create
an Interface called InventoryRepository.java.
The interface was extended by JpaRepository and passed the Inventory Model and data type of its Id
number.
JpaRepository is JPA specific extension of Repository. It contains the full API
of CrudRepository and PagingAndSortingRepository. So it contains API for basic CRUD operations and
also API for pagination and sorting.
5. Creating a Service (Business Layer )
We mark beans with @Service to indicate that it’s holding the business logic.
Here we are going to implement the functions like Insert, retrieve, update and delete the Product
records. We wrote the business logic in the service class file.
Now create a package called service and under the service package create a class
called InventoryService.java.
6. Creating a Controller
We @Autowired the InventoryService class and called the methods for crud operations called
InventoryController.java.
7. Testing
Now we can test the REST APIs using software called Postman.
Before running the application make sure that you have created a database with the name that you have
given in the application.properties file. In my case inventory.
Part 2. Product Microservice
1. Initialise the project
We are going to create the Product Sevice. Again go to spring intializer and specify your
own Groud Id, Artifact Id, Project Name, Java Version and Dependencies.
✓ Spring Web — To include Spring MVC and embedded Tomcat into your project
✓ Mongo DB – JDBC Driver
✓ Lombok — Java library tool that generates code for minimizing boilerplate code. The library
replaces boilerplate code with easy-to-use annotations
2. Connect Spring Boot Application With the database
Under the resources folder, you can see a file called application.properties. Inside the
application.properties file add those details. We use MongoDB as the database therefore we have to add
the connection string.
spring.data.mongodb.uri=put the connection string here
server.port=8081
For example:
3. Creating a Product Model ( Domain Model )
As previously We will create a package for models. Inside the model package, create a class call
Product.java
4. Creating Repository Classes ( Persistence Layer )
Next, let’s create a package for repository classes. Inside the package create
an Interface called ProductRepository.java.
MongoRepository extends the PagingAndSortingRepository and QueryByExampleExecutor
interfaces that further extend the CrudRepository interface. MongoRepository provides all the
necessary methods which help to create a CRUD application and it also supports the custom derived
query methods.
5. Creating a Service ( Business Layer )
We will create a package called dto and create two classes called Inventory.java and
InventoryResponse.java.
Here We are going to call a inventory service endpoint in product service. For that we need RestTemplate
for that.
Rest Template is used to create applications that consume RESTful Web Services. You can use
the exchange(), getForObject etc methods to consume the web services for all HTTP methods. The code
given below shows how to create Bean for Rest Template to auto wiring the Rest Template object. Let’s
go to out main class and add the following.
To write the business logic in the service class file create a package called service and under the service
package create a class called ProductService.java.
Here you can see in the fetchPdoductById method we are calling an endpoint of inventory service in
order to get the details of a invntory using the Id.
Now create a controller package and inside that create a class called ProductController and add the below
code.
6. Testing
Now you can run the application and test all the endpoints with a help of tool like Postman.