@PathVariable @RequestHeader @ModelAttribute @RequestParam @MatrixVariable @cookievalue @RequestBody @RequestAttribute
@PathVariable 用于获取路径中的参数
像映射路径 @GetMapping("/user/{id}/owner/{username}") 可以使用@PathVariable注解获取大括号中的id和usernane的值@PathVariable("id") String id, //获取路径中的id @PathVariable("username") String username, //获取路径中的username
@RequestHeader 用于获取请求的头信息
@RequestHeader("User-Agent") String header, //获取的单个User-Agent头信息 @RequestHeader Mapheaders, //获取 所有的头信息 获取所有的头信息使用map集合并且里面都是String类型
头信息还有很多,可以F12在RequestHeader获取想要的信息
@RequestParam 用于获取携带的请求参数
和@PathVariable做区分,此注解用于获取例如
localhost:8080/user/1/owner/zz?age=15&inters=play%20basketball&inters=watch%20moving若要获取所有的请求参数,可以使用map
其中?后面的 age和inters对应的值是使用@RequestParam注解获取,而id和username是使用@PathVariable获取
@cookievalue 用于获取cookie
@cookievalue("_ga") String _ga, //获取_ga的cookie的值 @cookievalue("_ga") cookie cookies){ //获取所有_ga的cookie的值其中可以直接用cookie获取cookies对象
@RequestBody 用于获取请求体,可以获取表单全部的数据
@ResponseBody //获取表单的数据 @RequestMapping("/loginjudge") public MapreBody(@RequestBody String content) { HashMap map = new HashMap<>(); map.put("content", content); return map; } 若要获取表单的单个数据可以使用 HttpServletRequest request 获取
String username = request.getParameter("username"); String password = request.getParameter("password");
@RequestAttribute 获取request域属性
在 HttpServletRequest 中setAttribute,在另外一个页面通过@RequestAttribute获取Attribute的值,为了取出请求域中的数据必须是转发,俩次获取的msg是同一个值.
另外还可以使用俩次 HttpServletRequest request,获取的同样也是一个值
@MatrixVariable 矩阵变量
SpringBoot默认是禁用了矩阵变量的功能,需要手动开启,开启方法:在Webconfig类中注入Bean
@Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; }queryString 风格 /user/{id}/owner?xxx=xxx&xxx=xxx xxx对应的值使用@RequestParam注解获取
rest风格: /user/{id}/xxx/xxx
矩阵变量风格:/user/{id};name=xxx;inters=xxx,xxx
矩阵变量,使用;来分割不同的请求参数
//user/1;name=zwz;inters=basketball,football @ResponseBody @GetMapping("/user/{id}") public Map carsSell(@MatrixVariable("name") String name, @MatrixVariable("inters") Listinters, @PathVariable("id") Integer id){ Map map = new HashMap<>(); map.put("name",name); map.put("inters",inters); map.put("id",id); return map; } //当路径中获取的矩阵变量有重名的使用pathVar指定在哪一层路径下的值 @GetMapping("/boss/{bossId}/{empId}") public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge, @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){ Map map = new HashMap<>(); map.put("bossAge",bossAge); map.put("empAge",empAge); return map; }



