在 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注解接收参数。
相关推荐
开开心心就好4 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei6 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
步行cgn7 小时前
Spring c 命名空间注入详解
java·后端·spring
百万蹄蹄向前冲8 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙8 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan10 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen10 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理11 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
IT_陈寒12 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端
需要82612 小时前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat