Building my first microservice (Chapter 1)

Before discussing the topic, let’s briefly understand what a microservice is.

A microservice is basically an independent component that provides a unique business capability.

Few microservices examples from an eCommerce business,

  • User microservice
  • Order microservice
  • Payment microservice
  • Reward microservice

All the above microservices are nothing but a different components. These components can talk to each other to perform one complex business scenario and complete the final job.

A microservice can be a service provider to another microservice OR can be a consumer from another microservice.

In this article, we will see how to start developing a simple microservice. This article is the first chapter of the series, which is only for absolute beginners. I may post multiple articles related to this topic. Also, I will not explain theories in detail. I suggest you go through some basics of microservice. Here is a link if needed.

I am using IntelliJ IDEA Ultimate, Java, Maven and Spring Boot for this project. All these must be installed on your laptop.

Step 01

Open the IntelliJ IDEA and create a Spring project. Then provide a name and a group name for your project and click Next.

Step 01

Step 02

Select Spring Web and Spring Boot Actuator dependencies and click on Create.

Step 02 Step 02 (2)

Step 03

Once you create the project, open the pom file. The pom file will show the Spring Web and Spring Boot Actuator dependencies. Then reload the pom file so that all dependencies will be downloaded to the project. Else, execute the “mvn clean install” command. This command will download all the required jars for your project.

Step 03

Before moving to Step 04, you must briefly be aware of the following layers related to service.

  • Controller: The controller is responsible for handling the traffic.
  • Service: The service layer is responsible for handling the business logic.
  • Repository: The repository layer deals with the database.

Step 04

Build the first controller, name it “MainController”, and put it inside a separate package. Also, annotate it with the “@RestController.”

Step 04 Step 04 (2)

Step 05

Build a first method inside the controller class. Here, I have created a simple method to return a String value.

Step 05

Before moving to Step 06, you must understand that this is a simple Java method. Now we are going to call this from the HTTP request. The HTTP request is a URL, and there should be a mapping. If somebody calls a specific URL, you have to return this. Step 06 will map the HTTP URL with the Java method.

Step 06

Map the HTTP request with the Java method using the “@RequestMapping” annotation. Here you have to set a value for the URL.

Step 06

In the above example, if someone calls the “/getValue” URL, the request will come to the “myFirstMethod” java method and execute the code.

Step 07

Now we are ready with a very simple application. Now just run the application.

Step 07

Step 08

Open any rest client to send a request to the service running on our local machine. I am using Postman to submit a request.

  • Create a new request.
  • Enter the local host URL as “http://localhost:8080
  • Enter the complete URL which we set in the Java method. So the final URL will be “http://localhost:8080/getValue
  • Select the request method as “GET” in the Postman and send the request.

You will see the String value returning from the Java method we created.

Step 08

Step 09

Change the port if needed. For this step, open the “application.properties” file inside the resources folder and change the post.

Step 09

If you send the same request from Postman without changing the port, you will see an error like this. Since we have already changed the port to 9001, we have to update the request.

Step 09 (2)

Port 8080

Step 09 (3)

Port 9001

Step 10

Send a “POST” request instead of a “GET” request and see the result.

Step 10

POST Request

As you can see, we are getting the response for the “POST” request the same as “GET”. This is because we are not controlling the request type in our controller.

Step 11

Control the response only for the “GET” method by adding the “RequestMethod.”

Step 11

Step 12

Now you will get an error if you send the request as a “POST” method. Now we only support “GET” requests.

Step 12

Step 13

Add a different response for the “POST” requests. You will see two different responses for “GET” and “POST” requests.

Step 13

Step 13 (2)

GET Request

Step 13 (3)

POST Request

Now we have a simple service. We will discuss more details in chapter 2.

That’s it for today, guys. Thank You for Reading! I hope you found this article informative and useful.

If you think it could benefit others, please share it on your social media networks with friends and family who might also appreciate it.