1、Get请求
1.1 方法形参接收参数
- 这种方式一般适用参数比较少的情况,并且前后端参数名称必须保持一致
@RestController@RequestMapping("/user")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(String name,String age){
log.info("name:{}",name);
log.info("age:{}",age);}}
- 参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController@RequestMapping("/user")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(@RequestParam(value ="name", required =false)String name,@RequestParam(value ="age", required =false)String age){
log.info("name:{}",name);
log.info("age:{}",age);}}
1.2 实体类接收参数
注意:Get 请求以实体类接收参数时,不能用 RequestParam 注解进行标注,因为不支持这样的方式获取参数。
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(Student student){
log.info("name:{}",student.getName());
log.info("age:{}",student.getAge());}}@DataclassStudent{privateString name;privateInteger age;}
1.3 通过HttpServletRequest接收参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(HttpServletRequest request){String name = request.getParameter("name");String phone = request.getParameter("age");
log.info("name:{}",name);
log.info("age:{}",age);}}
1.4 通过@PathVariable接收参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@GetMapping("/query/{name}/{age}")publicvoidgetStudent(@PathVariableString name,@PathVariableString age){
log.info("name:{}",name);
log.info("age:{}",age);}}
1.5 接收数组参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(String[] names){Arrays.stream(names).forEach(System.out::println);}}
1.6 接受集合参数
注意:SpringBoot 接收集合参数,必须用 @RequestParam 注解声明!
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@GetMapping("/query")publicvoidgetStudent(@RequestParamList<String> names){
names.forEach(System.out::println);}}
2、POST请求
2.1 方法形参接收参数
- 前后端参数名称必须保持一致
@RestController@RequestMapping("/user")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(String name,String age){
log.info("name:{}",name);
log.info("age:{}",age);}}
- 参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController@RequestMapping("/user")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(@RequestParam(value ="name", required =false)String name,@RequestParam(value ="age", required =false)String age){
log.info("name:{}",name);
log.info("age:{}",age);}}
2.2 通过HttpServletRequest接收参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(HttpServletRequest request){String name = request.getParameter("name");String phone = request.getParameter("age");
log.info("name:{}",name);
log.info("age:{}",age);}}
2.3 通过@PathVariable接收参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save/{name}/{age}")publicvoidsaveStudent(@PathVariableString name,@PathVariableString age){
log.info("name:{}",name);
log.info("age:{}",age);}}
2.4 通过param方式提交参数,以实体类接收参数
直接以实体类可以接收param、form-data、 x-www-form-urlencoded 提交的参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(Student student){
log.info("name:{}",student.getName());
log.info("age:{}",student.getAge());}}@DataclassStudent{privateString name;privateInteger age;}
2.5 请求体以JSON格式提交参数,通过 @RequestBody 注解接收参数
- 接受实体类JSON参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(@RequestBodyStudent student){
log.info("name:{}",student.getName());
log.info("age:{}",student.getAge());}}@DataclassStudent{privateString name;privateInteger age;}
- 接收数组或者集合类JSON参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(@RequestBodyString[] names){Arrays.stream(names).forEach(System.out::println);}@PostMapping("/save")publicvoidsaveStudent(@RequestBodyList<String> names){
names.forEach(System.out::println);}}
2.6 通过 Map 接收参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(@RequestParamMap<String,Object> map){
log.info("name:{}",map.get("name"));
log.info("age:{}",map.get("age"));}}
2.7 通过@RequestBody 接收一个参数
@RestController@RequestMapping("/demo")@Slf4jpublicclassDemoController{@PostMapping("/save")publicvoidsaveStudent(@RequestBodyString name){
log.info("name:{}",name);}}
本文转载自: https://blog.csdn.net/qq1743177790/article/details/142656797
版权归原作者 香饽饽~、 所有, 如有侵权,请联系我们删除。
版权归原作者 香饽饽~、 所有, 如有侵权,请联系我们删除。