当前位置: 代码迷 >> 综合 >> 关于 @PathVariable @RequestParam @RequestBody
  详细解决方案

关于 @PathVariable @RequestParam @RequestBody

热度:71   发布时间:2024-02-02 09:23:14.0

@PathVariable @RequestParam 的get请求:

    @GetMapping("/test1/{id}")public String show(@PathVariable(value = "id") String stuId,@RequestParam(value = "name",required = true)String name,@RequestParam(value = "address",required = false)String address){String result=stuId+name+address;return result;}

利用postman请求成功:

在这里插入图片描述

由于对于name设置成必须,所以没有name则请求失败!
在这里插入图片描述

而address没有设置成必须,所以正常访问!

在这里插入图片描述
post和get 一样

在这里插入图片描述

@RequestBody

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

    @PostMapping("/test2")public Student show1(@RequestBody Student student){return student;}

传递json

return student;
}


传递json
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200729143341282.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzM5MzM2MzI4,size_16,color_FFFFFF,t_70#pic_center)
  相关解决方案