Quickstart Json RESTful microservice with Spring-Boot

Quickstart Json RESTful microservice with Spring-Boot

February 02, 2017 ( last updated : February 03, 2017 )
spring-boot quickstart rest json microservice cloudnative development



In this post you will learn how to create a RESTful microservice from scratch using Spring-Boot, Eclipse & Maven.

Spring boot is one of the very popular frameworks to build microservices. In fact it has been chosen to be the standard for JBoss Fuse (Fuse Integration Services 2.0) deployments on Openshift. It can also be used for general java application purposes. You would be surprised how easy it is to create standalone packages that are capable of running production grade services.

Source code for this can be found here : https://github.com/alainpham/rest-service

These are the steps of this tutorial


As a prerequisite for this tutorial you will need the following elements:

Create a simple maven project

First you must create a simple maven project. For that you can use the Eclipse Wizard

create new maven project

Chose simple project to have an empty pom.xml file

choose simple project in eclipse

Fill in the project details

fill project infos

Edit the pom.xml file

Add these parent, dependencies, and plugin blocs to your pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





Create Application class

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);


Create a business model

package com.app.model;

public class Person {

	private Long id;
	private String name;

	public Long getId() {
		return id;
	public void setId(Long id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;


Create a service

package com.app.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

import com.app.model.Person;

public class PersonService {

	Map<Long, Person> personMap;

	public PersonService() {
		personMap = new HashMap< Long, Person>();

		Person p1 = new Person();
		p1.setName("John Doe");

		Person p2 = new Person();
		p2.setName("Jane Smith");

		personMap.put(p1.getId(), p1);
		personMap.put(p2.getId(), p2);

	@RequestMapping(value="/{id}",method = RequestMethod.GET)
	public Person getPerson(@PathVariable Long id){
		return personMap.get(id);

Run your application

Open a command prompt in the root folder of your project and run :

mvn spring-boot:run

You can now also build your package and run the flat jar file

mvn package
java -jar target/rest-service-0.0.1-SNAPSHOT.jar

Now open your browser and consume your microservice by entering the following url

test rest service by consuming

The next post will be about how to deploy such microservices packaged in Docker containers on Openshift

Thanks for reading !

Originally published February 02, 2017
Latest update February 03, 2017

Related posts :