关于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