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

全部评论

相关推荐

07-18 14:34
门头沟学院 Java
感觉招聘要求好高,这都是招什么人才
电气电子小朋友:你都说了这是大疆,无人机顶级企业,薪资30k-50k,要求高是正常的
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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