16.普通参数与基本注解
1注解:
@PathVariable、 restful风格的参数
@RequestHeader、 请求头中的参数
@ModelAttribute、 ...
@RequestParam、 获取请求参数
@MatrixVariable、 解析矩阵变量
@CookieValue、 解析Cookie
@RequestBody 解析请求体 表单提交post
下方注解中的map 可以将全部的参数自动放入map中
@RestController public class ParameterTestController { // car/2/owner/zhangsan @GetMapping("/car/{id}/owner/{username}") public Map<String,Object> getCar(@PathVariable("id") Integer id, @PathVariable("username") String name, @PathVariable Map<String,String> pv, @RequestHeader("User-Agent") String userAgent, @RequestHeader Map<String,String> header, @RequestParam("age") Integer age, @RequestParam("inters") List<String> inters, @RequestParam Map<String,String> params, @CookieValue("_ga") String _ga, @CookieValue("_ga") Cookie cookie){ Map<String,Object> map = new HashMap<>(); // map.put("id",id); // map.put("name",name); // map.put("pv",pv); // map.put("userAgent",userAgent); // map.put("headers",header); map.put("age",age); map.put("inters",inters); map.put("params",params); map.put("_ga",_ga); System.out.println(cookie.getName()+"===>"+cookie.getValue()); return map; } //这个RequestBody 只能这么写,写其他的会报错,因为数据格式是这样的:content="username=111&password=111"
//public String tologin(@RequestBody String content,String username,String password),这种的数据格式就必要了,直觉用实体类 简单方便
//username=123&password=456
//123
//456
@PostMapping("/save") public Map postMethod(@RequestBody String content){ Map<String,Object> map = new HashMap<>(); map.put("content",content); return map; } //1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd //2、SpringBoot默认是禁用了矩阵变量的功能 // 手动开启:原理。对于路径的处理。UrlPathHelper进行解析。 // removeSemicolonContent(移除分号内容)支持矩阵变量的 //3、矩阵变量必须有url路径变量才能被解析 @GetMapping("/cars/{path}") public Map carsSell(@MatrixVariable("low") Integer low, @MatrixVariable("brand") List<String> brand, @PathVariable("path") String path){ Map<String,Object> map = new HashMap<>(); map.put("low",low); map.put("brand",brand); map.put("path",path); return map; } // /boss/1;age=20/2;age=10 @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; } }
@RequestAttribute
@RequestAttribute 注解的作用 比如将处理好的数据放入请求域中 跳转到其他页面是一次请求 就比如做验证,暴露的接口提供验证,验证成功后 内部转发做跳转到其他页面 但是做一次请求中,所以直接用httpservletrequest 拿也是可以的