SpringBoot中Get请求和POST请求接收参数示例详解
1、Get请求
1.1 方法形参接收参数 这种方式一般适用参数比较少的情况,并且前后端参数名称必须保持一致
@RestController @RequestMapping("/user") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(String name,String age) { log.info("name:{}",name); log.info("age:{}",age); } }
参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController @RequestMapping("/user") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(@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") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(Student student) { log.info("name:{}",student.getName()); log.info("age:{}",student.getAge()); } } @Data class Student{ private String name; private Integer age; }
1.3 通过HttpServletRequest接收参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(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") @Slf4j public class DemoController { @GetMapping("/query/{name}/{age}") public void getStudent(@PathVariable String name, @PathVariable String age) { log.info("name:{}",name); log.info("age:{}",age); } }
1.5 接收数组参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(String[] names) { Arrays.stream(names).forEach(System.out::println); } }
1.6 接受集合参数
注意:SpringBoot 接收集合参数,必须用 @RequestParam 注解声明!
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(@RequestParam List<String> names) { names.forEach(System.out::println); } }
2、POST请求
2.1 方法形参接收参数 前后端参数名称必须保持一致
@RestController @RequestMapping("/user") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(String name,String age) { log.info("name:{}",name); log.info("age:{}",age); } }
参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController @RequestMapping("/user") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(@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") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(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") @Slf4j public class DemoController { @PostMapping("/save/{name}/{age}") public void saveStudent(@PathVariable String name, @PathVariable String age) { log.info("name:{}",name); log.info("age:{}",age); } }
2.4 通过param方式提交参数,以实体类接收参数
直接以实体类可以接收param、form-data、 x-www-form-urlencoded 提交的参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(Student student) { log.info("name:{}",student.getName()); log.info("age:{}",student.getAge()); } } @Data class Student{ private String name; private Integer age; }
2.5 请求体以JSON格式提交参数,通过 @RequestBody 注解接收参数
接受实体类JSON参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(@RequestBody Student student) { log.info("name:{}",student.getName()); log.info("age:{}",student.getAge()); } } @Data class Student{ private String name; private Integer age; }
接收数组或者集合类JSON参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(@RequestBody String[] names) { Arrays.stream(names).forEach(System.out::println); } @PostMapping("/save") public void saveStudent(@RequestBody List<String> names) { names.forEach(System.out::println); } }
2.6 通过 Map 接收参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(@RequestParam Map<String,Object> map) { log.info("name:{}",map.get("name")); log.info("age:{}",map.get("age")); } }
2.7 通过@RequestBody 接收一个参数
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @PostMapping("/save") public void saveStudent(@RequestBody String name) { log.info("name:{}",name); } }
到此这篇关于SpringBoot中Get请求和POST请求接收参数详解的文章就介绍到这了,更多相关SpringBoot Get请求和POST请求接收参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springboot在filter中如何用threadlocal存放用户身份信息
这篇文章主要介绍了springboot中在filter中如何用threadlocal存放用户身份信息,本文章主要描述通过springboot的filter类,在过滤器中设置jwt信息进行身份信息保存的方法,需要的朋友可以参考下2024-07-07Spring JPA事务管理与自定义操作实例解析(最新推荐)
在Spring框架中,数据持久化操作常常与事务管理紧密相关,本文将深入探讨Spring Data JPA中的事务管理机制,并结合具体实例,展示如何自定义事务行为以满足不同的业务需求,感兴趣的朋友一起看看吧2024-12-12
最新评论