使用注解 @RequestParam 和 @PathVariable 在 Postman 测试中一些注意事项

@PathVariable

路径占位符,用于定义路径变量。

    @DeleteMapping("/deleteUser/{id}")
    public RespBean deleteUser(@PathVariable Integer id){
        if (userService.deleteUser(id) == 1){
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

这个 delete 请求方法的 URL 是这样的:http://localhost:8080/deleteUser/22,这个 22 就是前端传的 id 值。

在 postman 测试中,如下图,直接将 id 值写在 URL 中

@RequestParam

@RequestParam 是传递参数的,用于将请求参数映射到 URL 中。

    @DeleteMapping("/deleteUser")
    public RespBean deleteUser(@RequestParam Integer id){
        if (userService.deleteUser(id) == 1){
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

这个 delete 请求方法的 URL 是这样的:http://localhost:8080/deleteUser?id=23,这个 23 就是前端传的 id 值。

在 postman 测试中,将 id 参数以 key-value 的形式,写在 params 中:

两者的效果是一样的,同样删除了一个指定的 user。

参考资源

https://blog.csdn.net/a15028596338/article/details/84976223

posted @   爱吃西瓜的番茄酱  阅读(3644)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
历史上的今天:
2020-08-09 Spring Cloud 框架 -- Spring Cloud Gateway
2020-08-09 Spring Cloud 框架 -- Zuul
2020-08-09 报错:Failed to read artifact descriptor for org.springframework.cloud:spring-cloud-starter-netflix-zuul:jar:2.2.2.RELEASE
2020-08-09 Spring Cloud 框架 -- Resilience4j
点击右上角即可分享
微信分享提示