SpringBoot获取参数的几种方式
直接把表单参数写在Controller对应的方法的形参中
// 适用于get方式提交,不适用于post方式提交 @RequestMapping("/addUser1") public String addUser1(String username,String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
通过HttpServletRequest接收
// get和post方式都可以 @RequestMapping("/addUser2") public String addUser2(HttpServletRequest request) { String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
通过一个Bean来接收
// get和post方式都可以 @RequestMapping("/addUser3") public String addUser3(UserModel user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "demo/index"; }
通过@PathVariable获取路径中的参数
// 例如:访问http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111 @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET) public String addUser4(@PathVariable String username,@PathVariable String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
通过@ModelAttribute注解获取Post请求的Form表单数据
// 不做此注释也能拿到user对象 @RequestMapping(value = "/helloWorld") public String helloWorld(@ModelAttribute User user) { return "helloWorld"; }
使用@RequestParam绑定请求参数到方法入参
// 当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false) @RequestMapping(value="/addUser5",method=RequestMethod.POST) public String addUser4(@RequestParam(value="username", required=false) String username,@RequestParam(value="password", required=false) String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
注解详解
@PathVariable
通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) ,请求的路径中需要与占位符的参数一一对应
@RequestParam
可将请求参数绑定至方法参数, 此时要求controller方法中的参数名称要跟form中参数名称一致,该注解有三个属性:
- value:请求参数名,必须配置
- required:是否必须,默认为true,即请求参数中必须包含该参数,如果没包含,将会抛出异常
- defaultValue:默认值,如果设置了该值,required将自动设为false,无论你是否配置了required,配置了什么值,都是 false
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。提交方式为get或post。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
RequestParam实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。
get方式中query String的值,和post方式中body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到。
@RequestBody
@RequestBody注解可以接收json格式的数据,并将其转换成对应的数据类型。 最常见的就是接收一个json格式的实体对象;
该注解处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
GET请求中,因为没有HttpEntity,所以@RequestBody并不适用。
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。@RequestBody用于post请求,不能用于get请求
@ModelAttribute
该注解类型将参数绑定到Model对象 ,form表单中的参数name要和实体的属性名对应。
有点类似于@RequestBody接收实体,只不过不能接收json格式的数据