@RequestBody与@RequestParam

@RequestBody与@RequestParam

这两个注解容易记混,还有@PathVariable,

前端post请求,后端需要加注解@RequestBody,否则参数传不到后端,试了一下午,血的教训🤬

https://blog.csdn.net/ChineseSoftware/article/details/118413481

 

1.  使用@RequestBody注解,前端需要传json字符串,传String会报错

 

2. 前端直接传String类型,后端无法接收到参数

3.前端传json,后端不加@RequestBody注解,直接报500。

4前端传json成功

 

5不可以传String,String类型不能直接传,String要怎么传?

 

@PathVariable

@PathVariable是Rest风格衍生出的占位符,只支持一个属性value,类型是为String,代表绑定的属性名称。默认不传递时,绑定为同名的形参。 用来便捷地提取URL中的动态参数。应用时,在@RequestMapping请求路径中,将需要传递的参数用花括号{}括起来,然后,通过@PathVariable("参数名称")获取URL中对应的参数值。如果@PathVariable标明参数名称,则参数名称必须和URL中参数名称一致。

                                            /{a}/{c}/{c}
@GetMapping("/dish/{id}")
public Result dish(@PathVariable Long id) {
List<SetmealVO> list = setmealService.getSetmealDishBySetmealId(id);
return Result.success(list);
}
 
 
可以传String
 

posted on 2024-01-21 19:20  hellowworld!  阅读(23)  评论(0编辑  收藏  举报

导航