Book Image

Spring Cookbook

Book Image

Spring Cookbook

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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) {
    this.id = id;
    this.name = name;
    this.age = age;
  }

  // ... getters and setters
}

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

public class UserService...