Chapter 6 Building RESTful Services
6.1 Introduction to RESTful
6.2 Building a RESTful application interface
6.3 Using Swagger to generate Web API documents
6.4 Practice: Implementing Web API version control
6.2 Build a RESTful application interface
6.2.1 Spring Boot's support for RESTful
The components provided by Spring Boot spring-boot-starter-web
fully support the development of RESTful API, and provide annotations corresponding to REST operation methods (GET, POST, PUT, DELETE):
- @GetMapping: Handle GET requests to get resources.
- @PostMapping: Process POST requests and add resources.
- @PutMapping: Handle PUT requests and update resources.
- @DeleteMapping: Handle DELETE requests and delete resources.
- @PatchMapping: Handles PATCH requests for partial update of resources.
Through these annotations, you can easily build RESTful interfaces in Spring Boot projects. The more commonly used annotations are @GetMapping, @PostMapping, @PutMapping, @DeleteMapping.
Using Spring Boot to develop a RESTful interface is very simple. Define the controller through @RestController, and then use annotations such as @GetMapping and @PostMapping to define the address mapping and implement the corresponding resource operation methods.
Example:
RESTfulController.java
package com.example.restfulproject.controller;
import com.example.restfulproject.model.User;
import org.springframework.web.bind.annotation.*;
/**
* RESTful 接口简单案例(增删改查)
*/
@RestController
public class RESTfulController {
@GetMapping(value = "/user/{id}")
public String getUserById(@PathVariable String id) {
return "getUserById:" + id;
}
@PostMapping(value = "/user")
public String save(@RequestBody User user) {
String name = user.getName();
return "save successed";
}
@PutMapping(value = "/user")
public String update(@RequestBody User user) {
return "update successed";
}
@DeleteMapping(value = "/user/{id}")
public String delete(@PathVariable String id) {
return "delete id:" + id;
}
/*
// 上面的注解是 @RequestMapping 注解的简化:
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String getUserById(@PathVariable String id) {
return "getUserById:" + id;
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String save(User user) {
return "save successed";
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public String update(User user) {
return "update successed";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable String id) {
return "delete id:" + id;
}
*/
}
6.2.2 Spring Boot implements RESTful API
1. Design APIs
In the RESTful architecture, each URL represents a resource, so it is recommended not to include verbs in the URI, but only nouns, and the nouns used often correspond to the table names of the database.
(1) Interface definition
The following table is the interface definition of the user management module. The RESTful API document of the actual project should be more detailed, and the data structure of all requests will also be defined.
User Management Module API Description
HTTP Method | interface address | Interface Description |
---|---|---|
POST | /user | create user |
GET | /user/id | Get user information by id |
PUT | /user | update user |
DELETE | /user/id | Delete the corresponding user according to the id |
The interface of the user management module is defined in the above table. According to the definition of REST, we define a user as a resource, and realize the addition, deletion, modification and query of users through HTTP methods such as POST, DELETE, PUT, and GET.
(2) Definition of status code and prompt information
In addition to designing the URL interface, it is also necessary to define the status code and prompt information returned by the server to the client. See the table below for detailed status code descriptions.
Description of user management module 状态码
:
status code | status description |
---|---|
200 | Ok, the request was successful |
201 | Created, successfully added |
203 | Updated, modified successfully |
204 | Deleted, deleted successfully |
In addition to defining the business status codes related to user management, it is also necessary to define common error codes, such as 400 corresponding to data verification errors, 401 corresponding to data without permission, etc.
Description of user management module 错误码
:
error code | error description |
---|---|
400 | data validation error |
401 | No permission |
404 | resource does not exist |
500 | server error |
2. Implement user management interface
UserManagementController.java
package com.example.restfulproject.controller;
import com.example.restfulproject.comm.utils.JSONResult;
import com.example.restfulproject.m