Spring Boot Hello World REST Service

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.testrest;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
  @Autowired CustomerRepository repository;

  @RequestMapping("/list")
  public List<Customer> findAll() {
    return repository.getData();
  }

  @RequestMapping("/one/{id}")
  public Customer findOne(@PathVariable int id) {
    return repository.getData().get(id);
  }
}

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

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

More in detail, the CustomerController Endpoint has two methods:

  • findAll: which returns all the Customer objects
  • findOne: which returns a single Customer object

Both methods rely on the CustomerRepository Class which has a very simple implementation to produce a List of Customer objects:

package com.example.testrest;

import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Component
public class CustomerRepository {
  List<Customer> customerList = new ArrayList<Customer>();

  @PostConstruct
  public void init() {
    customerList.add(new Customer(1, "frank"));
    customerList.add(new Customer(2, "john"));
  }

  public List<Customer> getData() {
    return customerList;
  }
}

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/list, 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

The source code for this tutorial is available on GitHub: https://github.com/fmarchioni/masterspringboot/tree/master/helloworld

Would you like to learn how to test this example with REST Assured? so check this: Testing Spring Boot with REST Assured