Book Image


Book Image


Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
About the Author
About the Reviewers

Creating a REST service

REST uses a web service architecture; here, a client sends an HTTP request to a server, which sends back an HTTP response. JSON is most of the time used for data transfer. The list of URLs supported by the server is called the REST API. These URLs are kept simple using different HTTP methods. For example, the /users/3 request using the GET method will return the user whose ID is 3. The /users/3 request using the DELETE method will delete that same user.

In this recipe, we will create a simple REST service that will allow a REST client to query and modify a list of User objects on the server.

Getting ready

We will use the User class:

public class User {
  private Long id;
  private String name;
  private int age;
  public User() {

  public User(Long id, String name, int age) { = id; = name;
    this.age = age;

  // ... getters and setters

We will use the UserService class with a preset list of users:

public class UserService...