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 start.spring.io 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-web, spring-boot-starter-test, which provides the required basic dependencies to create a RESTful web 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.

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) {
        this.name = name;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

Creating the Spring Boot RESTful service

Create a resource controller class called MyRestController.java, 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;

@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

The final step is to create a class that provides the 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 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

Conclusion

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

RESTful Web service using Spring Boot
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading