1、@PathVariable
作用说明:获取路径参数
案例:
接口收参形式:
@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;}
请求传参形式:
http://127.0.0.1:8080/car/3/owner/lisi
返回结果:
{"pv": {"id": "3","username": "lisi"},"name": "lisi","id": 3
}
可以用Map<String,String>
接收全部路径参数。
2、@RequestHeader
作用说明:获取请求头参数
案例:
接口收参形式:
@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;}
请求传参形式:
http://127.0.0.1:8080/car/3/owner/lisi
返回结果:
{"headers": {"user-agent": "PostmanRuntime/7.26.8","accept": "*/*","cache-control": "no-cache","postman-token": "7651f635-068b-4fe6-8e26-4990ef714349","host": "127.0.0.1:8080","accept-encoding": "gzip, deflate, br","connection": "keep-alive"},"userAgent": "PostmanRuntime/7.26.8"
}
可以用Map<String,String>
接收全部Header
参数。
3、@RequestParam
作用说明:获取请求参数,就是以往的GET
请求传参方式。
案例:
接口收参形式:
@GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@RequestParam("age") Integer age,@RequestParam("inters") List<String> inters,@RequestParam Map<String,String> params){Map<String,Object> map = new HashMap<>();map.put("age",age);map.put("inters",inters);map.put("params",params);return map;}
请求传参形式:
http://127.0.0.1:8080/car/3/owner/lisi?age=18&inters=basketball&inters=game
返回结果:
{"inters": ["basketball","game"],"params": {"age": "18","inters": "basketball"},"age": 18
}
可以用Map<String,String>
接收全部GET
请求参数参数。
4、@RequestBody
作用说明:获取form
表单请求体参数,就是以往的POST
请求传参方式。
案例:
接口收参形式:
@PostMapping("/save")public Map postMethod(@RequestBody String content){Map<String,Object> map = new HashMap<>();map.put("content",content);return map;}
请求传参形式:
<form action="/save" method="post">用户名:<input name="userName"/> <br>邮箱:<input name="email"/><input type="submit" value="提交"/>
</form>
返回结果:
注意:
如果你用postman
测试接口。
那么,@RequestBody
收参时,对应的传参方式如下:
如果你想用postman
传递form-data
参数,就必须要用@RequestParam
注解,并且key
要对应上。
5、@CookieValue
作用说明:获取cookie
参数
案例:
接口收参形式:
@GetMapping("/car/{id}/owner/{username}")public Map<String,Object> getCar(@CookieValue("_ga") String _ga,@CookieValue("_ga") Cookie cookie){Map<String,Object> map = new HashMap<>();map.put("_ga",_ga);System.out.println(cookie.getName()+"===>"+cookie.getValue());return map;}
请求传参形式:
http://127.0.0.1:8080/car/3/owner/lisi?age=18&inters=basketball&inters=game
浏览器添加Cookie
Postman
添加cookie
返回结果:
{"_ga": "123456789"
}
6、@RequestAttribute
作用说明:页面转发时,用于获取request
属性。
案例:
接口收参形式:
转发接口
@GetMapping("/goto")public String goToPage(HttpServletRequest request){request.setAttribute("msg","成功了...");request.setAttribute("code",200);return "forward:/success"; //转发到 /success请求}
目标接口
@ResponseBody@GetMapping("/success")public Map success(@RequestAttribute(value = "msg",required = false) String msg,@RequestAttribute(value = "code",required = false)Integer code,HttpServletRequest request){Object msg1 = request.getAttribute("msg");Map<String,Object> map = new HashMap<>();map.put("reqMethod_msg",msg1);map.put("annotation_msg",msg);return map;}
请求传参形式:
http://127.0.0.1:8080/goto
返回结果:
{"reqMethod_msg": "成功了...","annotation_msg": "成功了..."
}
7、@MatrixVariable
作用说明:获取矩阵参数,如果浏览器禁用了cookie
,那么,就用它传递cookie
参数。
案例:
开启矩阵参数功能:
在配置类中,向IOC容器
注册一个WebMvcConfigurer
实例。
@Beanpublic WebMvcConfigurer webMvcConfigurer(){return new WebMvcConfigurer() {@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {UrlPathHelper urlPathHelper = new UrlPathHelper();// 不移除;后面的内容。矩阵变量功能就可以生效urlPathHelper.setRemoveSemicolonContent(false);configurer.setUrlPathHelper(urlPathHelper);}};}
接口收参形式:
形式1//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;}形式2// /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;}
请求传参形式:
形式1
http://127.0.0.1:8080/cars/sell;low=34;brand=byd,audi,yd
http://127.0.0.1:8080/cars/sell;low=34;brand=byd;brand=audi;brand=yd
形式2
http://127.0.0.1:8080/boss/1;age=20/2;age=10
返回结果:
结果1
{"path": "sell","low": 34,"brand": ["byd","audi","yd"]
}
结果2
{"bossAge": 20,"empAge": 10
}