在 Spring MVC 中,用于接收前端传递的参数的注解常用的有以下几种

目录

1、对于json请求体参数,

2、对于路径传参

3、对于query传参

4、对于form-data参数,

总结:


1、对于json请求体参数,

请求头的Content-Type应为application/json。在前端,可以使用data格式传参。在后端,可以使用**@RequestBody**注解来接收参数。

javascript 复制代码
 this.$axios({
           method: 'post',
           url: 'http://localhost:8080/api/upload/editGoods',
           data: {
              id: this.id,
              name: this.name,
              price: this.price

                }
            }).then((res) => {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
            })
java 复制代码
   @GetMapping("/editGoods")
    public Result editGoods(@RequestBody Goods goods) {
        return uploadFileService.editGoods(goods);
    }

2、对于路径传参

(例如:test/111/2222),请求头不需要设置Content-Type。在前端,可以将参数通过URL的方式传递(例如:url=/api/upload/test2/111/2222)。在后端,可以使用**@PathVariable**注解来接收参数。

javascript 复制代码
 this.$axios({
          method: 'post',
          url: 'http://localhost:8080/api/user/deleteUser/' + userId,
        }).then((res) => {
          this.$message({
            message: res.data.message,
            type: "success",
          });
          // 刷新表格数据
          this.selectUser();
        });
      }).catch(() => {
        // 用户点击了取消按钮
        // 执行取消操作或不执行任何操作
      });
java 复制代码
    @PostMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable String userId) {

        return userservice.deleteUser(userId);
    }

3、对于query传参

(例如:test3?id=11111&name=222222),请求头也不需要设置Content-Type。在前端,可以将参数通过URL的方式传递(例如:url=/api/upload/test3?id=11111&name=222222)。在后端,可以使用**@RequestParam**注解来接收参数。

javascript 复制代码
this.$axios({
  method: 'post',
  url: 'http://localhost:8080/api/user/deleteUser',
  params: {
    userId: userId
  }
}).then((res) => {
  this.$message({
    message: res.data.message,
    type: "success",
  });
  // 刷新表格数据
  this.selectUser();
}).catch(() => {
  // 用户点击了取消按钮
  // 执行取消操作或不执行任何操作
});
java 复制代码
 //params传参
    @GetMapping("/editGoods");
    public String editGoods(@RequestParam String id, @RequestParam  String name) {
        System.out.println(id);
        System.out.println(name);

        return id;
    }

4、对于form-data参数,

请求头的Content-Type应为multipart/form-data。在前端,可以使用params格式传参。在后端,可以使用**@RequestParam**注解来接收参数。

javascript 复制代码
  this.$axios({
           method: 'post',
           url: 'http://localhost:8080/api/upload/editGoods',
           params: {
                 id: this.id,
                 name: this.name,

              }
            }).then((res) => {
                this.$message({
                    message: '修改成功',
                    type: 'success'
                })
            })
java 复制代码
 //params传参
    @GetMapping("/editGoods");
    public String editGoods(@RequestParam String id, @RequestParam  String name) {
        System.out.println(id);
        System.out.println(name);

        return id;
    }

query传参和form-data传参,后端接收是一样的

总结:

    • form-data参数使用multipart/form-data作为Content-Type,前端使用params格式传参,后端使用@RequestParam注解接收参数。
    • json请求体参数使用application/json作为Content-Type,前端使用data格式传参,后端使用@RequestBody注解接收参数。
    • 路径传参不需要设置Content-Type,前端将参数通过URL传递,后端使用@PathVariable注解接收参数。
    • query传参也不需要设置Content-Type,前端将参数通过URL传递,后端使用@RequestParam注解接收参数。
相关推荐
初遇你时动了情3 分钟前
前端使用TensorFlow.js reactjs调用本地模型 实现图像、文本、音频/声音、视频相关识别
前端·javascript·tensorflow
广州华水科技9 分钟前
单北斗GNSS变形监测系统安装与应用解析,提升位移监测精度
前端
J***Q29211 分钟前
前端微前端框架原理,qiankun源码分析
前端·前端框架
菜鸟‍12 分钟前
【前端学习】React学习【万字总结】
前端·学习·react.js
百***844519 分钟前
Webpack、Vite区别知多少?
前端·webpack·node.js
Mintopia23 分钟前
零信任架构下的 WebAIGC 服务安全技术升级方向
前端·人工智能·trae
敏姐的后花园2 小时前
模考倒计时网页版
java·服务器·前端
AiXed3 小时前
PC微信WDA算法
前端·javascript·macos
没有bug.的程序员6 小时前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
一只小阿乐9 小时前
react 封装弹框组件 传递数据
前端·javascript·react.js