Developing microservice with java

Microservice architecture is another trending service oriented architecture. I have another post about microservices architecture.


Java is one of the most used language for back-end development which is suitable for developing microservices. There are different ways of developing java microservices with either containerized technologies or non-containerized one. In this post I have described these two approaches.

Java applications can be running either in a web server or be self-contained with an embedded server. You can decide either ways according to the architectural decision that is made in an organization regarding different parameters:

  • Runtime environment:
    • containerized
    • non-containerized
  • API management
  • Development strategies

You can choose web server based technologies such as Jax-rs to develop your microservice whose end-result is generating a war file deployable to a web-server. The other approach is using spring-boot with a self-contained tomcat server which is bundled in a jar file including both application and a server. The jar file is separately runnable on a machine with a java run-time environment. I am gonna show both jax-rs and spring-boot approach in the following sections.


Project structure:


Every java application needs a build framework such as maven, graddle, ant, etc. I am going to use maven the most used build and dependency management tool for java. In order to develop a spring boot you need to include the following dependencies in your pom.xml file.

        <version>_Brixton_.RELEASE</version>  <!-- Name of release train -->
            <!-- Setup Spring Boot -->

            <!-- Setup Spring MVC & REST, use Embedded Tomcat -->

            <!-- Spring Cloud starter -->

            <!-- Eureka for service registration -->

Since spring boot is self-contained and produces a runnable jar file, we need the java main method to run our application. Then the following class is required to be added to the project:

package hello;

import java.util.Arrays;

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

public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx =, args);
        System.out.println("Let's inspect the beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName : beanNames) {




The next step is to create a controller:

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

public class HelloController {
    public String index() {
        return "Greetings from Spring Boot!";