API 参数传递问题

简单传参 restful @PathVariable {} 映射参数

    @GetMapping("/{userId}")
    public Result getIndexData(@PathVariable(required = true) String userId) {}

但是使用时就只能通过url映射,json格式不行了

示例 http://localhost:8080/v1/api/index/7f9a8b9c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b

@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

全部评论

相关推荐

强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务