RESTful Web service using Spring Boot

Creating a RESTful web service using the spring boot framework is very easy and takes very little time.

In this article, we will learn how to create a simple Spring Boot RESTful web service. Our service receives HTTP GET requests and returns the JSON formatted response to the 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.

Version Details:

  • Spring Boot 2.0.3.RELEASE

Table of Contents

Create Maven Project and add dependency entries to POM file

Create a new Maven project on Eclipse IDE and modify the pom.xml file to the content as shown below.

Alternatively, we can use the website to generate the project structure.

Here, we are using spring-boot-starter-parent as a parent project, which downloads all required spring dependency jars.

We have also added spring-boot-starter-webspring-boot-starter-test, which provides the required basic dependencies to create a RESTful web application.

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

Create a representation class

Create a class called under package com.asb.model.

The created java class 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;

Creating the Spring Boot RESTful service

Create a resource controller class called, which accepts HTTP requests from clients and responds with JSON responses. Add a request mapping method to handle incoming resource requests. 

We are adding a request mapping /api/v1/ to our controller class, which returns greeting messages, along with an auto-incremented message Id in the response message.

The API listens to the incoming requests on the endpoint /getmessage.

We have also restricted the controller method to accept only HTTP 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

The final step is to create a class that provides the 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 a java application from your eclipse IDE by selecting Right Click on the java class > Run as > Java application.

Running the java class should start our REST application listening to HTTP GET requests at the endpoint http://localhost:8080/api/v1/getmessage.

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

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

Spring boot REST example

Also, hitting the REST endpoint with a parameter returns the below response data.

Spring boot REST example with parameter


In this article, we learned how to create a simple Spring Boot RESTful web service.