一篇文章弄懂Spring MVC的参数绑定
作者:java_lover 时间:2023-09-17 01:01:21
前言
参数绑定,简单来说就是客户端发送请求,而请求中包含一些数据,那么这些数据怎么到达 Controller ?这在实际项目开发中也是用到的最多的,那么 SpringMVC 的参数绑定是怎么实现的呢?
下面我们来详细的讲解。
SpringMVC参数绑定,简单来说就是将客户端请求的key/value数据绑定到controller方法的形参上,然后就可以在controller中使用该参数了
下面通过5个常用的注解演示下如何进行参数绑定:
1. @PathVariable注解
@PathVariable 是用来获得请求url中的动态参数的,可以将URL中的变量映射到功能处理方法的参数上,其中URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
示例代码:
@ResponseBody
@RequestMapping("/testUrlPathParam/{param1}/{param2}")
public void testUrlPathParam(HttpServletRequest request, @PathVariable String param1,
@PathVariable String param2) {
System.out.println("通过PathVariable获取的参数param1=" + param1);
System.out.println("通过PathVariable获取的参数param2=" + param2);
}
Postman发送请求截图:
输出结果:
通过PathVariable获取的参数param1=1
通过PathVariable获取的参数param2=2
2.@RequestHeader注解
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
示例代码:
@ResponseBody
@RequestMapping("/testHeaderParam")
public void testHeaderParam(HttpServletRequest request, @RequestHeader String param1) {
System.out.println("通过RequestHeader获取的参数param1=" + param1);
}
Postman发送请求截图:
输出结果:
通过RequestHeader获取的参数param1=abc
3.@CookieValue注解
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。
示例代码:
@ResponseBody
@RequestMapping("/testCookieParam")
public void testCookieParam(HttpServletRequest request, HttpServletResponse response,
@CookieValue String sessionid) {
System.out.println("通过CookieValue获取的参数sessionid=" + sessionid);
}
Postman发送请求截图:
输出结果:
通过CookieValue获取的参数sessionid=ebef978eef6c46f8a95cc0990d2d360a
4.@RequestParam注解
@RequestParam注解用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。提交方式为get或post。(Http协议中,form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded);
@RequestParam注解实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段,
get方式中queryString的值,和post方式中body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到;
该注解有三个属性: value、required、defaultValue; value用来指定要传入值的id名称,required用来指示参数是否必录,defaultValue表示参数不传时候的默认值。
示例代码:
@ResponseBody
@RequestMapping("/testRequestParam")
public void testRequestParam(HttpServletRequest request,
@RequestParam(value = "num", required = true, defaultValue = "0") int num) {
System.out.println("通过RequestParam获取的参数num=" + num);
}
Postman发送请求截图:
输出结果:
通过RequestParam获取的参数num=10
5.@RequestBody注解
@RequestBody注解用来处理HttpEntity(请求体)传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据;
GET请求中,因为没有HttpEntity,所以@RequestBody并不适用;
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上。
示例代码:
@ResponseBody
@RequestMapping("/testRequestBody")
public void testRequestBody(HttpServletRequest request, @RequestBody String bodyStr){
System.out.println("通过RequestBody获取的参数bodyStr=" + bodyStr);
}
Postman发送请求截图:
代码运行结果:
通过RequestBody获取的参数bodyStr=这是body的内容
来源:https://www.cnblogs.com/haha12/p/10336363.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#通过指针读取文件的方法
android加密参数定位实现方法
C#识别出图片里的数字和字母
![](https://img.aspxhome.com/file/2023/0/78180_0s.png)
Android绘制机器人小实例
![](https://img.aspxhome.com/file/2023/8/138798_0s.jpg)
Spring Boot JPA如何把ORM统一起来
Java超详细教你写一个银行存款系统案例
![](https://img.aspxhome.com/file/2023/4/62434_0s.png)
利用flutter实现炫酷的list
![](https://img.aspxhome.com/file/2023/7/138027_0s.gif)
SpringBoot 利用thymeleaf自定义错误页面
![](https://img.aspxhome.com/file/2023/4/60054_0s.png)
java实现支付宝支付接口的调用
![](https://img.aspxhome.com/file/2023/1/58611_0s.jpg)
WPF实现带筛选功能的DataGrid
![](https://img.aspxhome.com/file/2023/0/76430_0s.png)
Android studio 去除版本控制教程
![](https://img.aspxhome.com/file/2023/3/113323_0s.jpg)
Spring 应用中集成 Apache Shiro的方法
Linux中Java开发常用软件安装方法总结
![](https://img.aspxhome.com/file/2023/2/85912_0s.png)
Android组件TabHost实现页面中多个选项卡切换效果
![](https://img.aspxhome.com/file/2023/6/138426_0s.jpg)
一篇文章读懂Java哈希与一致性哈希算法
![](https://img.aspxhome.com/file/2023/6/110906_0s.png)
MyBatis动态SQL标签用法实例详解
Avalonia封装实现指定组件允许拖动的工具类
![](https://img.aspxhome.com/file/2023/8/80078_0s.gif)
Android 代码一键实现银行卡绑定功能
![](https://img.aspxhome.com/file/2023/8/138458_0s.jpg)
SpringCloud学习笔记之OpenFeign进行服务调用
![](https://img.aspxhome.com/file/2023/0/62830_0s.png)
Python调用Java可执行jar包问题
![](https://img.aspxhome.com/file/2023/9/102899_0s.png)