Write Microservice in less than 2 mins.. (Faster than Maggi)

Sagar Somwanshi
Javarevisited
Published in
4 min readJun 13, 2022

Now a days Microservices architecture(MSA) is gaining lots of popularity in the software development.

What if I claim, we can write micro-service/Rest API in just 2 mins ….is it possible ?

Copied Image

I am not doing any magic ..This magic is done by Spring Data REST starter dependency for the Java developers.

Spring Data REST = Spring HATEOAS + Spring Data JPA

Pre-requisite :

  • Spring boot 2.7.0
  • Maven 3.6 +
  • Java 8 or later

Without wasting the time we will start writing Spring boot based Rest API /Service

Please go to https://start.spring.io/. Please add below dependencies in the project .

spring-boot-starter-web

spring-boot-starter-data-jpa

spring-boot-starter-data-rest

lombok

H2

spring-boot-devtools

Project Structure :

project Structure for reference

Product.java — Entity bean to store product information.
ProductRepository.java — Extends JpaRepository. Acts as a @RepositoryRestResource to provide RESTful API with CRUD Operations
data.sql — We use data.sql to populate the initial product data.
ProductMicroserviceApplication.java — The main Spring Boot Application class which is used to launch up the application.
pom.xml — Contains all the dependencies needed to build this project.

Code Snippet for Reference:

Spring boot Code for Microservice

Spring boot application.properties for Reference :

application.properties

Lets run the application and see the magic of Spring Data Rest:

Project –>Run As –> Spring Boot App.

Below snapshot represent the CRUD operation for Microservices . I have used Postman for sending request.

GET : http://localhost:9090
http://localhost:9090/products
GET http://localhost:9090/products
GET http://localhost:9090/products/2
POST http://localhost:9090/products
GET http://localhost:9090/products
PUT http://localhost:9090/products
H2 Database Snippet

We can even get pagination and sorting feature out of the box without writing single line of code

http://localhost:9090/products?page=0&size=2

output :

{
“_embedded”: {
“products”: [
{
“name”: “Laptop”,
“price”: 53000,
“status”: false,
“quantity”: 23,
“_links”: {
“self”: {
“href”: “
http://localhost:9090/products/1
},
“product”: {
“href”: “
http://localhost:9090/products/1
}
}
},
{
“name”: “Desktop”,
“price”: 35000,
“status”: true,
“quantity”: 34,
“_links”: {
“self”: {
“href”: “
http://localhost:9090/products/2
},
“product”: {
“href”: “
http://localhost:9090/products/2"
}
}
}
]
},
“_links”: {
“first”: {
“href”: “
http://localhost:9090/products?page=0&size=2
},
“self”: {
“href”: “
http://localhost:9090/products?page=0&size=2"
},
“next”: {
“href”: “
http://localhost:9090/products?page=1&size=2"
},
“last”: {
“href”: “
http://localhost:9090/products?page=1&size=2"
},
“profile”: {
“href”: “
http://localhost:9090/profile/products"
}
},
“page”: {
“size”: 2,
“totalElements”: 4,
“totalPages”: 2,
“number”: 0
}

http://localhost:9090/products?&sort=name&page=1&size=2

Output:

{
“_embedded”: {
“products”: [
{
“name”: “Mobile”,
“price”: 17540,
“status”: true,
“quantity”: 12,
“_links”: {
“self”: {
“href”: “
http://localhost:9090/products/3
},
“product”: {
“href”: “
http://localhost:9090/products/3
}
}
},
{
“name”: “TV”,
“price”: 89000,
“status”: false,
“quantity”: 50,
“_links”: {
“self”: {
“href”: “
http://localhost:9090/products/4
},
“product”: {
“href”: “
http://localhost:9090/products/4"
}
}
}
]
},
“_links”: {
“first”: {
“href”: “
http://localhost:9090/products?page=0&size=2&sort=name,asc
},
“prev”: {
“href”: “
http://localhost:9090/products?page=0&size=2&sort=name,asc"
},
“self”: {
“href”: “
http://localhost:9090/products?sort=name&page=1&size=2"
},
“last”: {
“href”: “
http://localhost:9090/products?page=1&size=2&sort=name,asc"
},
“profile”: {
“href”: “
http://localhost:9090/profile/products"
}
},
“page”: {
“size”: 2,
“totalElements”: 4,
“totalPages”: 2,
“number”: 1
}
}

Please download the code from here .

--

--

Sagar Somwanshi
Javarevisited

Technical Architect | AWS Certified | Micro-services developer | Spring boot | Docker | Jenkins | Oauth2.0 | WSO2 AM | Swagger 2.0|System Design