1
1
package com .caidapao .web ;
2
2
3
- import org .springframework .web .bind .annotation .RequestMapping ;
4
- import org .springframework .web .bind .annotation .RestController ;
3
+ import com .caidapao .entity .User ;
4
+ import io .swagger .annotations .ApiImplicitParam ;
5
+ import io .swagger .annotations .ApiImplicitParams ;
6
+ import io .swagger .annotations .ApiOperation ;
7
+ import org .springframework .web .bind .annotation .*;
8
+
9
+ import java .util .*;
5
10
6
11
@ RestController
7
12
@ RequestMapping ("/user" )
8
13
public class UserController {
9
- }
14
+ //创建线程安全的Map
15
+ private static Map <Long , User > users = Collections .synchronizedMap (new HashMap <>());
16
+
17
+ /**
18
+ * GET请求
19
+ */
20
+ @ ApiOperation (value = "获取用户列表" , notes = "获取所有用户" )
21
+ @ RequestMapping (value = "/" , method = RequestMethod .GET )
22
+ public List <User > getUserList () {
23
+ return new ArrayList <>(users .values ());
24
+ }
25
+
26
+ /**
27
+ * POST请求
28
+ * ModelAttribute注解自动将参数中的值映射到User对应的属性下,
29
+ * 如 :localhost:8080/users?name=123&sex=男 将会映射到user对象的 name和sex字段中
30
+ * 此处的 ModelAttribute注解也可以省略。
31
+ */
32
+ @ ApiOperation (value = "创建一个用户1【错误示范,不需要加上 @ApiImplicitParam】" , notes = "根据user对象创建用户" )
33
+ @ ApiImplicitParam (name = "user" , value = "用户详细实体user" , required = true , dataType = "User" )
34
+ @ RequestMapping (value = "/postUserView1" , method = RequestMethod .POST )
35
+ public String postUserView1 (@ ModelAttribute User user ) {
36
+ users .put (user .getId (), user );
37
+ return "user_detail" ;
38
+ }
39
+
40
+ /**
41
+ * POST请求
42
+ * ModelAttribute注解自动将参数中的值映射到User对应的属性下,
43
+ * 如 :localhost:8080/users?name=123&sex=男 将会映射到user对象的 name和sex字段中
44
+ * 此处的 ModelAttribute注解也可以省略。
45
+ */
46
+ @ ApiOperation (value = "创建一个用户2" , notes = "根据user对象创建用户" )
47
+ @ RequestMapping (value = "/postUserView2" , method = RequestMethod .POST )
48
+ public String postUserView2 (@ ModelAttribute User user ) {
49
+ users .put (user .getId (), user );
50
+ return "user_detail" ;
51
+ }
52
+
53
+
54
+ /**
55
+ * GET请求
56
+ * 通过PathVariable 注解绑定URL参数
57
+ * 如:localhost:8080/users/12 12将会绑定到id这个形参上
58
+ *
59
+ * @param id 用户id
60
+ */
61
+ @ RequestMapping (value = "/{id}" , method = RequestMethod .GET )
62
+ public User getUser (@ PathVariable Long id ) {
63
+ return users .get (id );
64
+ }
65
+
66
+ /**
67
+ * PUT 请求,意为修改、或者覆盖
68
+ *
69
+ * @param id 用户Id
70
+ * @param user 用户
71
+ */
72
+ @ ApiOperation (value = "更新用户信息" , notes = "根据url的id来指定更新的对象,并根据传过来的user信息来更新用户详细信息" )
73
+ @ ApiImplicitParams ({
74
+ @ ApiImplicitParam (name = "id" , value = "用户ID" , required = true , dataType = "Long" ),
75
+ @ ApiImplicitParam (name = "user" , value = "用户详细实体user" , required = true , dataType = "User" )
76
+ })
77
+ @ RequestMapping (value = "/{id}" , method = RequestMethod .PUT )
78
+ public String putUser (@ PathVariable Long id , @ ModelAttribute User user ) {
79
+ User u = users .get (id );
80
+ u .setName (user .getName ());
81
+ u .setSex (user .getSex ());
82
+ users .put (id , u );
83
+ return "user_detail" ;
84
+ }
85
+
86
+ /**
87
+ * DELETE请求
88
+ *
89
+ * @param id 用户id
90
+ */
91
+ @ RequestMapping (value = "/{id}" , method = RequestMethod .DELETE )
92
+ public String deleteUser (@ PathVariable Long id ) {
93
+ users .remove (id );
94
+ return "user_detail" ;
95
+ }
96
+
97
+ /**
98
+ * POST请求
99
+ * 使用RequestBody注解来接受参数,大都数用来接收JSON格式的参数
100
+ *
101
+ * @param user 用户
102
+ */
103
+ @ RequestMapping (value = "/save" , method = RequestMethod .POST )
104
+ public List <User > saveUser (@ RequestBody User user ) {
105
+ users .put (user .getId (), user );
106
+ return new ArrayList <>(users .values ());
107
+ }
108
+
109
+ }
0 commit comments