(十二) - REST 开发
REST 是一种开发风格, 遵从此风格开发软件, 符合 REST 风格, 则为 RESTFUL.
REST 有两个核心要求:
- 每个资源都有唯一的标识(URL);
- 不同的行为, 使用对应的 http-method (get, post, put, delete).
以下实例说明 REST 开发:
创建 User 类:
public class User { private Integer id; String name; @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } public User(Integer id, String name) { this.id = id; this.name = name; } public User() { } public Integer getId() { return id; } public String getName() { return name; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } }
创建 testRest.jsp: (会根据请求方法和URL去匹配handler)
<html> <head> <title>REST</title> <script src="js/jquery-3.5.1.js"></script> </head> <body> <input type="button" value="queryAll" onclick="queryAll()";> <input type="button" value="queryOne" onclick="queryOne()";> <input type="button" value="addUser" onclick="addUser()";> <input type="button" value="updateUser" onclick="updateUser()";> <input type="button" value="deleteUser" onclick="deleteUser()";> <script> function queryAll(){ $.ajax({ type: "get", url: "${pageContext.request.contextPath}/users", success: function (ret) { console.log("查询所有: "); console.log(ret); } }) } function queryOne() { $.ajax({ type: "get", url: "${pageContext.request.contextPath}/users/1", success: function (ret) { console.log("查询单个用户: "); console.log(ret); } }) } function addUser() { var user = {id: 13, name: "欧阳冰冰"}; $.ajax({ type: "post", url: "${pageContext.request.contextPath}/users", data: JSON.stringify(user), contentType: "application/json", success: function (ret) { console.log("增加用户: "); console.log(ret); } }) } function updateUser() { var user = {id: 1, name: "东方改名"}; $.ajax({ type: "put", url: "${pageContext.request.contextPath}/users", data: JSON.stringify(user), contentType: "application/json", success: function (ret) { console.log("更新用户: "); console.log(ret); } }) } function deleteUser() { $.ajax({ type: "delete", url: "${pageContext.request.contextPath}/users/2", success: function (ret) { console.log("删除用户: "); console.log(ret); } }) } </script> </body> </html>
创建 MyRestController.java:
@RestController public class MyRestController { // @GetMapping注解使该handler只能被get方法访问到, 其他同理 @GetMapping("/users") public List<User> queryUsers(){ System.out.println("query users with get"); User user1 = new User(1, "パソコン"); User user2 = new User(2, "李明"); return Arrays.asList(user1, user2); } @GetMapping("/users/{id}") public User queryOne(@PathVariable Integer id){ System.out.println("query one user with get: id=" + id); return new User (2, "李明"); } @DeleteMapping("/users/{id}") public String deleteOne(@PathVariable Integer id){ System.out.println("delete one user with delete: id=" + id); return "OK"; } @PostMapping("/users") public String addUser(@RequestBody User user){ System.out.println("add one user with post: " + user); return "OK"; } @PutMapping("/users") public String updateUser(@RequestBody User user){ System.out.println("update user with put: " + user); return "OK"; } }
测试及返回:
----------------------------------------------------------------------------
-------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------