How to Build a RESTful Web Service with Spring Boot

Spring is one of the most widely used frameworks for developing enterprise applications, providing a robust programming and configuration model. This guide walks you through the process of creating a “Hello, World” RESTful web service with Spring Boot.

To start a new Spring project you can use your favorite IDE (Eclipse, Intelij) or just follow the steps below to manually initialize the project:

  1. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.
  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
  3. Click Dependencies and select Spring Web.
  4. Click Generate.
  5. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

Create a Resource Representation Class

Now that you have set up the project and build system, you can create your web service.

  1. Go to New-> Package and create a new Package with name: model
  2. Next create a public Class HelloWorld.java
  3. Create two fields: long id and String message
  4. Generate getter and setters and constructor with the fields.

public class HelloWorld {
	private long id;
	private String message;
	
	public HelloWorld(long id, String message) {
		super();
		this.id = id;
		this.message = message;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
}

Tip: You can use lombok for generating getter and setters.

Create a Resource Controller

In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by the @RestController annotation, and the HelloWorld shown in the following listing (from src/main/java/net/lirent/restservice/HelloWorld.java) handles GET requests for /hello by returning a new instance of the HelloWorld class:

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	private static final String msg = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();

	@GetMapping("/hello")
	public HelloWorld hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Hello(counter.incrementAndGet(), String.format(msg, name));
	}
}

The @GetMapping annotation ensures that HTTP GET requests to /hello are mapped to the hello() method.

The implementation of the method body creates and returns a new HelloWorld object with id and content attributes based on the next value from the counter and formats the given name by using the text in msg field.

Run and Test the Service

You can run the application from the command line. You can also build a single executable JAR file that contains all the necessary dependencies, classes, and resources and run that. Building an executable jar makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.

Now that the service is up, visit http://localhost:8080/hello, where you should see:

{"id":1,"content":"Hello, World!"}

Provide a name query string parameter by visiting http://localhost:8080/hello?name=User. Notice how the value of the content attribute changes from Hello, World! to Hello, User!, as the following listing shows:

{"id":2,"content":"Hello, User!"}

You have just developed a RESTful web service with Spring

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *