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;
@GetMapping("user") public User getUser() { return null; }
@GetMapping("userByPath/{id}") public String getUserByPath(@PathVariable("id") String id) { return id; }
@PostMapping("/addUser") public User addUser(User user) { return user; }
@PostMapping("/addUserBody") public User addUserBody(@RequestBody User user) { return user; }
@PostMapping("/addUserstr") public User addUserString(String username, String password) { User user = User.builder().username(username).password(password).build(); return user; }
@DeleteMapping("/deleteUsers") public HashMap<String, Object> deleteUsers(@RequestBody UserDelDto userDelDto) { HashMap<String, Object> res = new HashMap<>(); res.put("obj", userDelDto);
return res; }
@DeleteMapping("/deleteUsers1") public HashMap<String, Object> deleteUsers1(@RequestBody String[] ids) { HashMap<String, Object> res = new HashMap<>(); res.put("obj", ids);
return res; }
@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; }
@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 {
@PostMapping("/save") public Map<String, String> save(@RequestBody Map<String,String> map) { return map; } }
|