在现代Web开发中,Spring Boot因其简洁性和高效性而广受欢迎。处理HTTP请求是Web应用的核心功能之一,其中GET和POST请求是最常用的两种类型。GET请求通常用于从服务器获取数据,而POST请求则用于向服务器发送数据。了解如何在Spring Boot中正确接收和处理这两种请求的参数,对于构建健壮的Web应用至关重要。本文将通过详细示例,解析Spring Boot中GET请求和POST请求接收参数的方法。
1、Get请求
1.1 方法形参接收参数 这种方式一般适用参数比较少的情况,并且前后端参数名称必须保持一致
@RestController@RequestMapping(\"/user\")@Slf4jpublicclassDemoController{@GetMapping(\"/query\")publicvoidgetStudent(Stringname,Stringage){log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController@RequestMapping(\"/user\")@Slf4jpublicclassDemoController{@GetMapping(\"/query\")publicvoidgetStudent(@RequestParam(value=\"name\",required=false)Stringname,@RequestParam(value=\"age\",required=false)Stringage){log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
1.2 实体类接收参数
注意:Get 请求以实体类接收参数时,不能用 RequestParam 注解进行标注,因为不支持这样的方式获取参数。
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@GetMapping(\"/query\")publicvoidgetStudent(Studentstudent){log.info(\"name:{}\",student.getName());log.info(\"age:{}\",student.getAge());}}@DataclassStudent{privateStringname;privateIntegerage;}
1.3 通过HttpServletRequest接收参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@GetMapping(\"/query\")publicvoidgetStudent(HttpServletRequestrequest){Stringname=request.getParameter(\"name\");Stringphone=request.getParameter(\"age\");log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
1.4 通过@PathVariable接收参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@GetMapping(\"/query/{name}/{age}\")publicvoidgetStudent(@PathVariableStringname,@PathVariableStringage){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(@RequestParamListnames){names.forEach(System.out::println);}}
2、POST请求
2.1 方法形参接收参数 前后端参数名称必须保持一致
@RestController@RequestMapping(\"/user\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(Stringname,Stringage){log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
参数用 @RequestParam 标注,使用value属性指定参数名,required属性表示这个参数是否必传
@RestController@RequestMapping(\"/user\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(@RequestParam(value=\"name\",required=false)Stringname,@RequestParam(value=\"age\",required=false)Stringage){log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
2.2 通过HttpServletRequest接收参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(HttpServletRequestrequest){Stringname=request.getParameter(\"name\");Stringphone=request.getParameter(\"age\");log.info(\"name:{}\",name);log.info(\"age:{}\",age);}}
2.3 通过@PathVariable接收参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save/{name}/{age}\")publicvoidsaveStudent(@PathVariableStringname,@PathVariableStringage){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(Studentstudent){log.info(\"name:{}\",student.getName());log.info(\"age:{}\",student.getAge());}}@DataclassStudent{privateStringname;privateIntegerage;}
2.5 请求体以JSON格式提交参数,通过 @RequestBody 注解接收参数
接受实体类JSON参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(@RequestBodyStudentstudent){log.info(\"name:{}\",student.getName());log.info(\"age:{}\",student.getAge());}}@DataclassStudent{privateStringname;privateIntegerage;}
接收数组或者集合类JSON参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(@RequestBodyString[]names){Arrays.stream(names).forEach(System.out::println);}@PostMapping(\"/save\")publicvoidsaveStudent(@RequestBodyListnames){names.forEach(System.out::println);}}
2.6 通过 Map 接收参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(@RequestParamMapmap){log.info(\"name:{}\",map.get(\"name\"));log.info(\"age:{}\",map.get(\"age\"));}}
2.7 通过@RequestBody 接收一个参数
@RestController@RequestMapping(\"/demo\")@Slf4jpublicclassDemoController{@PostMapping(\"/save\")publicvoidsaveStudent(@RequestBodyStringname){log.info(\"name:{}\",name);}}
总结
本文通过详细示例,深入解析了Spring Boot中GET请求和POST请求接收参数的方法。我们分别介绍了如何通过@RequestParam
注解接收GET请求的查询参数,以及如何通过@RequestParam
或@ModelAttribute
注解接收POST请求的表单数据。此外,我们还探讨了如何处理JSON格式的数据。通过这些示例,读者可以更好地理解Spring Boot在处理HTTP请求时的强大功能,并能够在实际项目中灵活运用这些技术。掌握这些技能,将有助于开发者构建更加高效和可靠的Web应用。