Here is an Hello World tutorial to create a simple REST Application using Spring Boot.

Representational State Transfer (REST) is basically an architectural style for the web. REST specifies a set of constraints. These constraints ensure that clients (service consumers and browsers) can interact with servers in flexible ways.

Let's first understand some common terminologies:

  • Server: Service provider. Exposes services which can be consumed by clients.
  • Client: Service consumer. Could be a browser or another system.
  • Resource: Any information can be a resource: a person, an image, a video, or a product you want to sell.
  • Representation: A specific way a resource can be represented. For example, the product resource can be represented using JSON, XML, or HTML. Different clients might request different representations of the resource.

Start by creating a new project which includes the web dependencies:

  spring init -dweb demo-rest

Now import the Maven project in your favourite IDE and add a model class named Customer that will be returned by a GET Request:

package com.example.demorest;

public class Customer {

	private int id;
	private String name;

	public Customer(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

All you need to build a Rest Service in Spring Boot is a RestController:

package com.example.demorest;

import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
 
@RestController
public class CustomerController
{
    @RequestMapping("/")
    public List<Customer> findAll()
    {
      List<Customer> customerList = new ArrayList<Customer>();
      customerList.add(new Customer(1, "frank"));
      customerList.add(new Customer(2, "john"));
      return customerList;
    }
}

RestController : The @RestController annotation provides a combination of @ResponseBody and @Controller annotations. This is typically used to create REST Controllers.

RequestMapping: The method with @RequestMapping would handle a generic request to the "/" URI. If not specified, it maps a GET Request.

Now compile and package the application with:

$ mvn clean install

Now you can run the application in any Spring Boot available way, f.e.

mvn spring-boot:run

If you surf on http://localhost:8080, the list of Customer in JSON format will be displayed:

Here is an Hello World tutorial to create a simple REST Application using Spring Boot

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials