这里给出了业务中常用的注解的使用方法。
涉及:@PathVariable(路径变量)、@RequestHeader(获取请求头)、@RequestParam(获取请求参数)、@CookieValue(获取cookie值)、@RequestBody(获取请求体·仅POST)、@RequestAttribute(获取request域属性)、@MatrixVariable(矩阵变量)
@PathVariable 路径变量
注解名 |
使用位置 |
说明 |
@PathVariable(“xxx”) |
方法传参 |
接收 Rest风格 请求路径中{xxx} 给后面的值 |
@PathVariable Map<String, String> |
方法传参 |
接收 Rest风格 请求路径中所有 {…},保存给Map |
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @GetMapping("/car/{id}/owner/{username}") public Map<String,Object> getCar(@PathVariable("id") Integer id, @PathVariable("username") String name, @PathVariable Map<String,String> pv){ Map<String,Object> map = new HashMap<>();
map.put("id",id); map.put("name",name); map.put("pv",pv); return map; }
|
访问 localhost/car/2/owner/zhangsan
可将
2
传给id
,
zhangsan
传给name
"id":"2", "name":"zhangsan"
传给pv
注解名 |
使用位置 |
说明 |
@RequestHeader(“key”) |
方法传参 |
获取 key 的请求头 给后面的 值 |
@RequestHeader Map<String, String> |
方法传参 |
获取 key 的所有请求头,保存给Map |
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| @GetMapping("/car/{id}/owner/{username}") public Map<String,Object> getCar(@RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String,String> header,){ Map<String,Object> map = new HashMap<>();
map.put("userAgent",userAgent); map.put("headers",header); return map; }
|
@RequestParam 请求参数
可以获取请求参数,带请求参数的URL示例
?后面给参数,不同参数用&连接。如果出现相同的 key 就会返回集合
1
| URL localhost/car/3/owner/lisi?age=18&inters=basketball&inters=game
|
注解名 |
使用位置 |
说明 |
@RequestParam(“key”) |
方法传参 |
接收 请求参数 赋值给后面的量 |
@RequestParam Map<String, String> |
方法传参 |
接收 所有请求参数 保存给Map |
示例
1 2 3 4 5 6
| ...... @RequestParam("age") Integer age, @RequestParam("inters") List<String> inters, @RequestParam Map<String,String> params, ......
|
@CookieValue Cookie值
注解名 |
使用位置 |
说明 |
@CookieValue(“key”) String |
方法传参 |
接收 Cookie 中的 key 给后面的 String |
@CookieValue Cookie |
方法传参 |
接收 所有Cookie 给Cookie |
@RequestBody 请求体
获取Post请求的请求体
注解名 |
使用位置 |
说明 |
@RequestBody(“key”) |
方法传参 |
接收 Cookie 中的 key 给后面的参数 |
1 2 3 4 5 6 7
| @PostMapping("/save") public Map postMethod(@RequestBody String content){ Map<String,Object> map = new HashMap<>(); map.put("content",content); return map; }
|
测试html
1 2 3 4 5 6
| <form action="/save" method="post"> 测试@RequestBody获取数据 <br/> 用户名:<input name="userName"/> <br> 邮箱:<input name="email"/> <input type="submit" value="提交"/> </form>
|
@RequestAttribute 获取request域属性
注解名 |
使用位置 |
说明 |
@RequestAttribute(value = “kay”) |
方法传参 |
接收请求域的值 后传 |
配合使用的方法
方法名 |
说明 |
setAttribute(“key”,”value”) |
设置请求域的值 (“k”,”v”) 方法属于HttpServletRequest |
getAttribute(“key”) |
获取请求域的指定key的值 方法属于HttpServletRequest |
测试用Contrller
PS:第9行 返回的return "forward:/success"
将整个请求都转发了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @Controller public class RequestController { @GetMapping("/goto") public String goToPage(HttpServletRequest request){
request.setAttribute("msg","成功了..."); request.setAttribute("code",200); return "forward:/success"; }
@ResponseBody @GetMapping("/success") public Map success(@RequestAttribute(value = "msg") String msg, //获取请求域中的 msg 的值,并传参 @RequestAttribute(value = "code")Integer code,//获取请求域的 code 的值,并传参 HttpServletRequest request){ Object msg1 = request.getAttribute("msg");
Map<String,Object> map = new HashMap<>(); Object hello = request.getAttribute("hello"); Object world = request.getAttribute("world"); Object message = request.getAttribute("message");
map.put("reqMethod_msg",msg1); map.put("annotation_msg",msg); map.put("hello",hello); map.put("world",world); map.put("message",message);
return map;
} }
|
@MatrixVariable 矩阵变量
矩阵变量的语法
- 和请求路径之间用
;
分隔
- 每个不同矩阵变量的
key
之间用;
分隔
- 相同
key
对应多个v
,用逗号分隔。也可以用分号分开写同名key
- 矩阵变量不作为路径变量的一部分
- 可以有多个路径变量绑定相同
key
的矩阵变量
示例:
localhost/cars/sell;low=34;brand=byd,audi,yd
矩阵变量使用要求
- 矩阵变量需要在SpringBoot中手动开启
- UrlPathHelper 解析路径时,removeSemicolonContent(移除分号内容)默认为true,所以默认矩阵变量就被丢弃了
- 矩阵变量需要绑定在路径变量中使用,直接用会404
矩阵变量的用法:
注解的使用
注解名 |
使用位置 |
说明 |
@MatrixVariable(“key”) xxx |
方法传参 |
接收 矩阵变量key和对应值 后传给xxx 当key由多个值,返回List |
@MatrixVariable(value = “key”,pathVar = “path”) xxx |
方法传参 |
当路径变量有多个时,就要使用pathVar 指定是哪一个路径变量的矩阵变量 |
开启矩阵变量功能
有两种方法,都是在配置类完成。
一种是:重写路径映射规则,创建一个自定的UrlPathHelper
1 2 3 4 5 6 7 8 9 10
| @Configuration(proxyBeanMethods = false) public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }
|
另一种:重新注册一个@Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Configuration(proxyBeanMethods = false) public class WebConfig{ @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; } }
|
Controller类示例
这里有两个路径变量,使用矩阵变量时需要指定pathVar
。
1 2 3 4 5 6 7 8 9 10 11 12
| @GetMapping("/boss/{bossId}/{empId}") public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge, @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
Map<String,Object> map = new HashMap<>();
map.put("bossAge",bossAge); map.put("empAge",empAge); return map;
}
|
如果只有一个路径变量就这么写:
@MatrixVariable("age") Integer age