1.@RequestParam
(1)前端发送请求参数为key=value的形式
例:http://localhost:8080/dish?ids=123,456
#
const deleteDish = (ids) => {
return $axios({
url: '/dish',
method: 'delete',
params: { ids }
})
}
(2)后端@RequestParam接收
@DeleteMapping("/dish")
public void delete(@RequestParam List<Integer> ids) {
for (Integer id : ids) {
QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("dish_id",id);
dishFlavorService.remove(queryWrapper);
}
}
2.@PathVariable
(1)前端发送请求参数为路径变量的形式(REST风格)
例:http://localhost:8080/dish/12345678
const queryDishById = (id) => {
return $axios({
url: `/dish/${id}`,
method: 'get'
})
}
(2)后端@PathVariable接收
@GetMapping("/dish/{id}")
public R<DishDto> getById(@PathVariable String id) {
DishDto dishDto = dishService.getByIdWithFlavor(id);
return R.success(dishDto);
}
3.@RequestBody
(1)前端发送请求参数为json类型
const addCategory = (params) => {
return $axios({
url: '/category',
method: 'post',
data: { ...params }
})
}
(2)后端@RequestBody接收
@PutMapping("/category")
public void update(@RequestBody Category category){
categoryService.updateById(category);
}