Spring Boot通过注解获取请求参数
spring boot通过注解获取请求参数
·
(1) @PathVariable:获取路径占位符的参数
- 将服务器中的请求参数,全部以请求路径的方式,拼接在路径中
- 通过@PathVariable注解,获取请求参数
* 原始方式: /deleteUser?id=1
* rest方式: /deleteUser/1
Controller:
//将占位符中的id的值,自动赋值给形参id; password的值赋值给形参password
@RequestMapping("/{id}/{password}")
public Map<String, Object> getInfo(
//获取单个占位符的参数
@PathVariable("id") Integer id,
@PathVariable("password") String password,
//获取占位符中的所有参数,保存在Map集合中
@PathVariable Map<String,String> pv) {
HashMap<String, Object> user = new HashMap<>();
user.put("id",id);
user.put("password", password);
user.put("pv",pv);
return user;
}
测试:
<a href="1/aaa">{id}/{password}参数获取@RequestParam</a>
(2) @RequestHeader:获取请求头信息
- @PathRequestHeader("User-Agent") 获取指定的请求头信息
- @PathRequestHeader 获取请求头的所有信息
//获取请求头信息
@RequestMapping("/header")
public Map<String, Object> getHeaderInfo(
//获取请求头指定的 User-Agent信息
@RequestHeader("User-Agent") String userAgent,
//获取请求头的所有信息,并保存在Map中
@RequestHeader Map<String, String> header) {
HashMap<String, Object> headerInfo = new HashMap<>();
headerInfo.put("User-Agent", userAgent);
headerInfo.put("header",header);
return headerInfo;
}
测试:
<a href="/header">通过@RequestHeader注解,获取请求头信息</a>
(3) @RequestParam:获取请求参数
功能: `将请求参数 和控制器方法的形参 创建映射关系`
- @RequestParam("参数名") 获取指定请求参数
- @RequestParam Map<String, String> 获取所有请求参数
控制器:
//获取请求参数
@RequestMapping("/params")
public Map<String, Object> getParamInfo(
//获取指定的请求参数,id
@RequestParam("id") Integer id,
//获取指定的请求参数,该参数为多个值
@RequestParam("interest") List<String> interest,
//获取所有请求参数,并保存在Map中
@RequestParam Map<String, String> params) {
HashMap<String, Object> paramInfo = new HashMap<>();
paramInfo.put("id", id);
paramInfo.put("params",params);
return paramInfo;
}
测试:
<a href="/params?id=1001&age=21&gender=0&interest=篮球&interest=羽毛球">
通过@RequestParam注解(获取请求参数)
</a>
结果:
{interest: ["篮球", "羽毛球"], id: 1001, params: {id: "1001", age: "21", gender: "0", interest: "篮球"}}
id: 1001
interest: ["篮球", "羽毛球"]
params: {id: "1001", age: "21", gender: "0", interest: "篮球"}
(4) @RequestBody
`将请求报文中的请求体,转换为Java对象`
- 只有post请求才有请求体
- 可以获取请求体,需要在控制器方法设置一个形参
- 使用@RequestBody标识,当前请求的请求体就会为形参赋值
控制器:
@PostMapping("requestBody")
//获取所有请求体信息
public Map postRequestBodyInfo(@RequestBody String bodyInfo) {
HashMap<String, Object> map = new HashMap<>();
map.put("bodyInfo", bodyInfo);
return map;
}
测试:
<form action="/requestBody" method="post">
用户名:<input name="username"/><br>
邮箱:<input name="email"/><br>
<input type="submit" value="通过@RequestBody注解(获取请求体信息)">
</form>
结果:
{"bodyInfo":"username=gong&email=2433634471%40qq.com"}
(5) @CookieValue
功能:`将cookie数据 和控制器方法的形参 创建映射关系`
- 通过控制器方法的形参,获取cookie信息
- @CookieValue("cookie名") 获取指定cookie的值
- @CookieValue Map<String, String> 获取所有cookie的值
控制器:
//获取请求参数
@RequestMapping("/cookie")
public Map<String, Object> getCookieInfo(
//获取指定的某个cookie的值
@CookieValue("_ga") String _ga,
//获取所有cookie的值,并保存在Map中
@CookieValue Map<String, String> cookies) {
HashMap<String, Object> cookieInfo = new HashMap<>();
cookieInfo.put("_ga", _ga);
cookieInfo.put("cookies", cookies);
return cookieInfo;
}
测试:
<a href="/cookie">通过@CookieValue注解(获取cookie信息)</a>
(6) @RequestAttribute
- 使用@RequestAttribute注解,获取request域对象中的共享数据
- @RequestAttribute("数据名")
@GetMapping("/success")
public Map success(
//通过@RequestAttribute注解,获取Request域对象中的数据
@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code) {
HashMap<String, Object> map = new HashMap<>();
//将获取的数据存放到Map中
map.put("annotation_msg", msg);
map.put("annotation_code", code);
return map;
}
结果:
{"annotation_msg":"成功。。。","annotation_code":200}
(7) 矩阵变量
原生传参:
/cars/sell?id=1004&brand=IBM&brand=Apple&brand=GOOGLE
矩阵变量方式:
/cars/sell;id=1004;brand=IBM,Apple,GOOGLE
更多推荐
所有评论(0)