RequestMapping参数传递

类似于这种参数传递http://localhost:8080/dish/status/0?ids=1397862477831122945

在上述URL中 /status/0?ids=1397862477831122945 其中0和后面的ids都是前端向后端传递的参数,前端代码部分如下:

// An highlighted block
// ajax请求
// 起售停售---批量起售停售接口
const dishStatusByStatus = (params) => {
  return $axios({
    url: `/dish/status/${params.status}`,
    method: 'post',
    params: { ids: params.id }
  })
}
// 页面代码
//状态更改
          statusHandle (row) {
            let params = {}
            if (typeof row === 'string' ) {
              if (this.checkList.length === 0) {
                this.$message.error('批量操作,请先勾选操作菜品!')
                return false
              }
              params.id = this.checkList.join(',')
              params.status = row
            } else {
              params.id = row.id
              params.status = row.status ? '0' : '1'
            }
            this.dishState = params
            this.$confirm('确认更改该菜品状态?', '提示', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
              'type': 'warning'
            }).then(() => {
              // 起售停售---批量起售停售接口
              dishStatusByStatus(this.dishState).then(res => {
                if (res.code === 1) {
                  this.$message.success('菜品状态已经更改成功!')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                  this.$message.error('请求出错了:' + err)
              })
            })
          },

从ajcx请求中可以看到这里传参的方式用到了两种方式,传status时直接将参数传过去,传ids时用属性的方式传递。
在这里如果想用Rest风格来接参数的话,后端则需要将前面的status作为路径参数来接受,第二个参数ids则可以使用自动装配的方式注入。后端controller如下:

// 后端参数接收
 @PostMapping("/status/{status}")
    public R<String> status(@PathVariable String status,Long ids){
    log.info("!!!!传参统计:status:"+status+" ids:"+ids);
}
// log如下:
2023-05-22 18:47:42.968  INFO 13308 --- [io-8080-exec-10] c.****.controller.DIshController   : !!!!传参统计:status:0 ids:1397862477831122945
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值