SpringBoot实战(二)Restful风格API接口

在上一篇SpringBoot实战(一)HelloWorld的基础上,编写一个Restful风格的API接口:

1.根据MVC原则,创建一个简单的目录结构,包括controller和entity,分别创建User对象和UserController控制器:

User类:

 1 package com.example.demo.controller.user.entity;
 2 
 3 public class User {
 4 
 5     private Integer id;
 6     private String name;
 7     private String password;
 8     private String phone;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password;
32     }
33 
34     public String getPhone() {
35         return phone;
36     }
37 
38     public void setPhone(String phone) {
39         this.phone = phone;
40     }
41 }

UserController

 1 package com.example.demo.controller.user.controller;
 2 
 3 import com.example.demo.controller.user.entity.User;
 4 import org.springframework.web.bind.annotation.*;
 5 
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 /**
10  * 通过RestController注解告知SpringBoot这是一个控制器类
11  * 通过RequestMapping注解说明统一处理以user开头的URL请求
12  */
13 @RestController
14 @RequestMapping("/user")
15 public class UserController {
16 
17     /**
18      * 获取用户列表
19      * @return
20      */
21     @GetMapping("/users")
22     public List<User> userList(){
23         List<User> result = new ArrayList<User>();
24 
25         for (int i = 0; i < 3; i++) {
26             User user = new User();
27             user.setId(i);
28             user.setName("name_"+i);
29             user.setPassword("password_"+i);
30             user.setPhone("phone_"+i);
31             result.add(user);
32         }
33         return result;
34     }
35 
36     /**
37      * 获取特定用户
38      * @param id
39      * @return
40      */
41     @GetMapping("/users/{id}")
42     public User getUser(@PathVariable(value="id") Integer id){
43 
44         User user = new User();
45         user.setId(id);
46         user.setName("name_"+id);
47         user.setPassword("password_"+id);
48         user.setPhone("phone_"+id);
49         return user;
50     }
51 
52     /**
53      * 添加用户
54      * @param user
55      * @return
56      */
57     @PostMapping("/add")
58     public String addUser(@RequestBody User user){
59 
60         return "添加成功";
61     }
62 
63     /**
64      * 修改用户
65      * @param id
66      * @param user
67      * @return
68      */
69     @PutMapping("/users/{id}/update")
70     public String updateUser(@PathVariable(value="id") Integer id,@RequestBody User user){
71         return "修改成功";
72     }
73 
74     /**
75      * 删除用户
76      * @param id
77      * @return
78      */
79     @DeleteMapping("/users/{id}/delete")
80     public String deleteUser(@PathVariable(value="id") Integer id){
81 
82         return "删除成功";
83     }
84 }

UserController包括了常用的Get、Post、Put、Delete请求,并使用注解的方式说明了请求路径

路径中的{id}元素是路径参数,可以通过@PathVariable注解获取,具体的参数获取与校验会在下一篇做介绍

使用Postman软件验证了所有接口都是可用的,部分截图如下:

Get:

Post

Put:

原文地址:https://www.cnblogs.com/anywherego/p/9599670.html