常用参数注解的使用
1、注解:
@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@CookieValue、@RequestBody
@PathVariable
@RequestHeader
@RequestParam
@CookieValue
@RequestBody
<ul>
<li>@PathVariable(路径变量)</li>
<li>@RequestHeader(获取请求头)</li>
<li>@RequestParam(获取请求参数)</li>
<li>@CookieValue(获取cookie值)</li>
<li>@RequestBody(获取请求体)[POST]</li>
</ul>
@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;
}
@PostMapping("/save")
public Map postMethod(@RequestBody String content){
Map<String,Object> map = new HashMap<>();
map.put("content",content);
return map;
}
@RequestAttribute
<li>@RequestAttribute(获取request域属性)</li>
<li>@MatrixVariable(矩阵变量)</li>
@GetMapping("/goto")
public String gotoPage(HttpServletRequest request){
request.setAttribute("message" ,"成功了");
return "forward:/success"; //转发到 /success 请求
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute(value = "message",required = false) String message,
HttpServletRequest request){
Object msg1 = request.getAttribute("message");
Object hello = request.getAttribute("hello");
Object world = request.getAttribute("world");
Object message1 = request.getAttribute("message1");
Map<String,Object> map = new HashMap<>();
map.put("reqMethod_msg",msg1);
map.put("annotation_msg",message);
map.put("hello",hello);
map.put("world",world);
map.put("message1",message1);
return map;
}
@MatrixVariable
<h3>矩阵变量</h3>
<a href="/cars/sell;low=34;brand=byd,audi,yd">@MatrixVariable (矩阵变量)</a>
<a href="/cars/sell;low=34;brand=byd;brand=audi;brand=yd">@MatrixVariable (矩阵变量)</a>
<a href="/boss/1;age=20/2;age=10">@MatrixVariable (矩阵变量) /boss/{bossId}/{empId}</a>
//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("boosAge",bossAge);
map.put("empAge",empAge);
return map;
}
2、Servlet API:
WebRequest、ServletRequest、MultipartRequest、 HttpSession、javax.servlet.http.PushBuilder、Principal、InputStream、Reader、HttpMethod、Locale、TimeZone、ZoneId
3、复杂参数:
Map、Model(map、model里面的数据会被放在request的请求域 request.setAttribute)、
Errors/BindingResult、RedirectAttributes( 重定向携带数据)、
ServletResponse(response)、SessionStatus、UriComponentsBuilder、ServletUriComponentsBuilder