Create Spring Boot REST API

In this tutorial, you will learn how to create a simple REST API using Spring boot 2.3.2 RELEASE.

What we will build?

You will build a service that will accept HTTP GET requests at http://localhost:8080/greeting.

It will respond with a JSON representation of a greeting, as the following listing shows:

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

You can customize the greeting with an optional name parameter in the query string, as the following listing shows:


The name parameter value overrides the default value of World and is reflected in the response, as the following listing shows:

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

We will implement this tutorial step by step.

Step 1: Create Spring Boot Project in Eclipse STS IDE

Use the below guide to create a Spring boot project in Eclipse STS IDE:

Step 2: Maven pom.xml File

The following listing shows the pom.xml file created when you choose Maven:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>






Step 3: Create a Resource Representation Class

To model the greeting representation, you create a resource representation class called Greeting. Provide a plain old java object with fields, constructors, and accessors for the id and content data:
public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) { = id;
         this.content = content;

    public long getId() {
        return id;

    public String getContent() {
         return content;
Spring uses the Jackson JSON library to automatically marshal instances of type Greeting into JSON.
Next, we create a resource controller that will serve these greetings.

Step 4: GreetingController

package net.javaguides.springboot.Springboothelloworldapplication;

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

public class GreetingController {

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

    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
         return new Greeting(counter.incrementAndGet(), String.format(template, name));

Step 5: Run Spring Boot Application

package net.javaguides.springboot.Springboothelloworldapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringbootHelloworldApplication {

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

From your IDE, run the SpringbootHelloworldApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser to http://localhost:8080/.

Or use the below command to run the Spring boot app:
$ mvn spring-boot:run

Now that the service is up, visit http://localhost:8080/greeting, where you see:
Provide a name query string parameter with http://localhost:8080/greeting?name=User. Notice how the value of the content attribute changes from "Hello, World!" to "Hello User!":