无@RequestBody获取数据为null

在进行POST提交时,遇到后端SpringBoot接收数据始终为null的问题。经过排查,问题出在后端缺少@RequestBody注解。在方法体上添加@RequestBody可以成功接收前端传递的数据,详情见知识点参考链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点参考:https://blog.youkuaiyun.com/justry_deng/article/details/80972817/
今天在做一个post提交数据时,发现后台接收到的数据总为null,

  • 这是vue前端代码
submitForm(formName) {
     this.ruleForm.pic='/img/singerPic/666.jpg';
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('http://localhost:8181/singer/addsinger',this.ruleForm).then(function (resp){
            if (resp.data!=null){
              _this.$message({
                message: '新增成功',
                type: 'success'
              });
            }
            else {
              console.log(resp)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  • 这是springboot后端
@PostMapping("/addsinger")
     public Singer addsinger(Singer singer){
         System.out.println(singer);
//         singerService.save(singer);
        return singer;
     }
  • 接收到的数据在这里插入图片描述
    刚开始并不知道为什么提交了数据为什么后台还是接收不到,起初我把方向转向了前端,是不是前端在获取表单的数据时并没有获取到表单的数据,所以控制台输出一下发现是有数据的。
this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          }
      });

前端有数据,后端接收为null,那么就是后端的问题,在网上查了资料发现有可能是没加@RequestBody的原因:

@PostMapping("/addsinger")
     public Singer addsinger(@RequestBody Singer singer){
         System.out.println(singer);
//         singerService.save(singer);
        return singer;
     }

在方法体上加上@RequestBody就可以解决问题了,关于@RequestBody的具体知识请参考:https://blog.youkuaiyun.com/justry_deng/article/details/80972817/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值