@RequestBody注解误区

it2022-05-05  76

前端代码:

// 加上params后,后台可以以@RequestParam的方式接收

delete = (id) => { console.log(id) // 加上params后,后台可以以@RequestParam的方式接收 axios.delete("/api/user/delete", { params:{empId:id} }).then(() => { console.log(id) message.success("删除成功!"); const { pagination } = this.state; pagination.current = 1; this.setState({ pagination }, this.getList); }).catch(err => { message.error("删除失败!"); }) }

// 此处以json的方式传输的话,在后台需要以json的方式接收

axios.post('http://localhost:8080/item/deleteItemById', { // 此处以json的方式传输的话,在后台需要以json的方式接收 'itemId':itemId }) .then((res) => { console.log("输出") console.log(res.data); if (res.data.code==100){ this.$message.success(res.data.msg) this.getList(this.currentPage,this.pageSize) } else{ this.$message.error(res.data.message) } }) .catch((error) => { console.log("错误信息") console.log(error); });

@RequestBody的作用其实是将json格式的数据转为java对象。

在前后台分离开发的过程中,如果后台的入参为null,说明前台已经请求到了后台的url接口。

通过F12对前台调试后,发现前台传的数据是json格式的,那么后台我们就需要用@RequestBosy注解来接收该数据,将其转化为Java对象。

最后经测试,成功!!!!愁死个鬼鬼,打卡,睡觉,晚安Java


最新回复(0)