API 参数传递问题
简单传参 restful @PathVariable {} 映射参数
@GetMapping("/{userId}")
public Result getIndexData(@PathVariable(required = true) String userId) {}
但是使用时就只能通过url映射,json格式不行了
@RequestParam ? 拼接参数
示例 http://localhost:8080/v1/api/index/api/user?userId=123
如果要使用 json格式 @RequestBody
不推荐
@PostMapping("/user")
public Result getIndexData(@RequestBody String userId) {
return Result.success(userId);
}
复杂 多样 @RequestBody+实体类接收
@PostMapping
private void insertUser(@RequestBody User user) {
userService.insertUser(user);
}
或者
使用 多个@RequestParam拼接
@PstMapping("/user")
public Result getIndexData(@RequestParam String userId) {
// 使用查询参数
return Result.success(userId);
}
示例 http://localhost:8080/v1/api/user``?``userId=123``?``xxx=xxx