本文主要是介绍SpringBoot中Get请求和POST请求接收参数示例详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《SpringBoot中Get请求和POST请求接收参数示例详解》文章详细介绍了SpringBoot中Get请求和POST请求的参数接收方式,包括方法形参接收参数、实体类接收参数、HttpServle...
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") php@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); China编程 log.info("age:{}",age); } }
1.2 实体类接收参数
注意:Get 请求以实体类接收参数时,不能用 RequestParam 注解进行标注,因为不支持这样的方式获取参数。
@RestController @RequestMapping("/demo") @Slf4j public class DemoController { @GetMapping("/query") public void getStudent(Student student) { log.info("naChina编程me:{}",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属性指http://www.chinasem.cn定参数名,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请求接收参数内容请搜索编程China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!
这篇关于SpringBoot中Get请求和POST请求接收参数示例详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!