RESTful Web service using Spring Boot

Creating a RESTful web service using spring boot is very easy and takes very little time. In this article, we will learn about how to create a simple RESTful web service that receives HTTP GET requests and returns JSON formatted responses back to clients.

We have used Eclipse IDE and Maven build tool to create our application on this post. We will create a RESTful service, which returns a JSON response, containing a message Id and a greeting message. Which will also allow us to send a request parameter while requesting the service.

Following are the steps to create a RESTFul web service using Spring Boot.

  • Create a new Maven Project and required dependency entries on pom.
  • Create a representation class to represent message object.
  • Create a resource controller class to handle REST requests.
  • Create Spring boot application class.

Create a new Maven Project and required dependency entries on pom :

Create new Maven project on Eclipse IDE and modify the pom.xml file to the content as shown below. Here, we are using spring-boot-starter-parent as a parent project, which downloads all required dependency jars. we have also added spring-boot-starter-web, spring-boot-starter-test, which provides basic required dependencies to our application.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Create a representation class :

Create a class called under package com.asb.model, which holds message Id an auto-incremented Id of type long and name field, which is passed as a parameter of  HTTP GET request.

package com.asb.model;

public class Message {
    private String name;
    private long id;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public long getId() {
        return id;
    public void setId(long id) { = id;

Create a resource controller class :

Create a resource controller class, which accepts HTTP requests from clients and responds with JSON responses. Create a class called as shown below and add a request mapping method to handle incoming resource requests. In this case, we are adding request mapping “/api/v1/” to our controller class and we have created a method, which returns greeting messages along with auto-incremented message Id in response format, listening on the endpoint “/getmessage“. We have restricted the controller method to accept only GET requests.

package com.asb;

import java.util.concurrent.atomic.AtomicLong;

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

import com.asb.model.Message;

public class MyRestController {

private final AtomicLong counter = new AtomicLong();

@RequestMapping(value="/getmessage", method = RequestMethod.GET)
public Message getResource(@RequestParam(value="name", defaultValue= "World!") String name) {
  Message msg = new Message();
  msg.setName("Hello "+ name);
  return msg;

Create Spring boot application class :

Final step is to create a class which provides entry point to our spring boot application. Create a class called and add the following code into it.

package com.asb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class RESTApplication {
 public static void main(String[] args) {, args);

Now run the above class as java application from your eclipse IDE by selecting Right Click on class > Run as > Java application. This should start our REST application listening to HTTP GET request at the end point http://localhost:8080/api/v1/getmessage.&nbsp;

We can also package our application with War or Jar format.

Following is the JSON output we get when we hit our end point on browser.

Spring boot REST example

Hitting REST end point with parameter.

Spring boot REST example with parameter

In this article, we learned how to create a simple REST Web service using spring boot.

Version Details:

  • Spring Boot 2.0.3.RELEASE
  • Eclipse Neon.2 Release (4.6.2)