springboot使用controller传递参数

:::tip

see https://github.com/yzqdev/spring-tutor/tree/dev/spring-transfer
额外内容 AntPathMatcher和PathPattern的区别
详细文档见spring.io
:::

基本传参方式

直接上代码

:::tip
注意
multipart/form-data与x-www-form-urlencoded的区别:
multipart/form-data:可以上传文件或者键值对,最后都会转化为一条消息
x-www-form-urlencoded:只能上传键值对,而且键值对都是通过&间隔分开的。
application/json: 上传的是json键值对
:::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
private final IUserService userService;

/**
* 获取用户
* api使用: http://localhost:9400/user/getUser
* @return {@link User}
*/
@GetMapping("user")
public User getUser() {
return null;
}

/**
* 使用pathvariable
* api使用: http://localhost:9400/userByPath/abcdde
*
* @param id id
* @return {@link String}
*/
@GetMapping("userByPath/{id}")
public String getUserByPath(@PathVariable("id") String id) {
return id;
}
/**
* 添加用户
* api使用: http://localhost:9400/user/addUserstr?username=aa&password=bb
* @return {@link User}
*/
@PostMapping("/addUser")
public User addUser(User user) {
return user;
}

/**
* 添加用户
* api使用: http://localhost:9400/user/addUserbody
*<pre>
* <code>
* {
* "username": "aa",
* "password": "bb"
* }
* </code>
*</pre>
* @param user 用户
* @return {@link User}
*/
@PostMapping("/addUserBody")
public User addUserBody(@RequestBody User user) {
return user;
}


/**
* 添加用户
*api使用: http://localhost:9400/user/addUserstr?username=aa&password=bb
* @param username 用户名
* @param password 密码
* @return {@link User}
*/
@PostMapping("/addUserstr")
public User addUserString(String username, String password) {
User user = User.builder().username(username).password(password).build();
return user;
}

/**
* 删除多个用户
* api使用: http://localhost:9400/user/deleteUsers
* <pre>
* <code>
* {
* "userIds": [
* "string"
* ]
* }
* </code>
* </pre>
* @param userDelDto 用户del dto
* @return {@link HashMap}<{@link String}, {@link Object}>
*/
@DeleteMapping("/deleteUsers")
public HashMap<String, Object> deleteUsers(@RequestBody UserDelDto userDelDto) {
HashMap<String, Object> res = new HashMap<>();
res.put("obj", userDelDto);

return res;
}

/**
* 删除users1
* api使用: http://localhost:9400/user/deleteUsers1
*[
* "string"
* ]
* @param ids id
* @return {@link HashMap}<{@link String}, {@link Object}>
*/
@DeleteMapping("/deleteUsers1")
public HashMap<String, Object> deleteUsers1(@RequestBody String[] ids) {
HashMap<String, Object> res = new HashMap<>();
res.put("obj", ids);

return res;
}

/**
* 获取请求头信息
*
* @return {@link HashMap}<{@link String}, {@link Object}>
*/
@GetMapping("/users")
public HashMap<String, Object> getUsers() {
HashMap<String, Object> res = new HashMap<>();
String token = RequestHelper.getRequestHeader("token");
String auth = RequestHelper.getRequestHeader("Authorization");
res.put("token", token);
res.put("auth", auth);
return res;
}

/**
* 检索
*api使用: http://localhost:9400/user/retrieve?username=aa
* @param username 用户名
* @return {@link User}
*/
@GetMapping("/retrieve")
public User retrieve(@RequestParam("username") String username) {
return userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
}

}

map传参方式(不推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@RestController
@RequestMapping("/my")
public class MyController {

/**
* 保存
* 用map接收
* api使用: http://localhost:9400/my/save
* <pre>
* <code>
* {
* "additionalProp1": "string",
* "additionalProp2": "string",
* "additionalProp3": "string"
* }
* </code>
* </pre>
* @param map 地图
* @return {@link Map}<{@link String}, {@link String}>
*/
@PostMapping("/save")
public Map<String, String> save(@RequestBody Map<String,String> map) {
return map;
}
}

Buy me a coffee
静听、唯美殇 微信 微信
静听、唯美殇 支付宝 支付宝