Creating a RESTful web service using spring boot is very easy and takes very less time. In this article, we will learn about how to create a simple RESTful web service which receives HTTP GET request and returns JSON formatted response back to client.

We have used Eclipse IDE and Maven build tool to create our application on this post. We will create a RESTful service, which returns JSON response, containing 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 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 to as shown below. Here, we are using spring-boot-starter-parent as 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="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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.asb</groupId>
  <artifactId>RESTExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

Create a representation class :

Create a class called Message.java under package com.asb.model, which holds message Id an auto incremented Id of type long and name field, which is passed as 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) {
        this.name = name;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

Create a resource controller class :

Create a resource controller class, which accepts HTTP requests from clients and responds with JSON response. Create a class called MyRestController.java 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 message along with auto incremented message Id in response format, listening on end point “/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;

@RestController
@RequestMapping("/api/v1/")
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.setId(counter.incrementAndGet());
  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 RESTApplication.java and add the following code into it.

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

@SpringBootApplication
public class RESTApplication {
 public static void main(String[] args) {
  SpringApplication.run(RESTApplication.class, 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. 

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)

Also Read: