常用參數注解的使用
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