路径变量@PathVariable
①获取指定路径变量:
@GetMapping("/car/{id}/owner/{userName}") public Map<String,Object> getCar(@PathVariable("id") int id, @PathVariable("userName") String userName){ Map<String,Object> map = new HashMap<>(); map.put("id",id); map.put("userName",userName); return map; }
请求:http://localhost:8080/car/1/owner/zhangsan
返回:{"id":"1","userName":"zhangsan"}
②获取所有路径变量:
//使用map提取所有的路径变量,map必须是String类型 @GetMapping("/car/{id}/owner/{userName}/{age}") public Map<String,String> getCar(@PathVariable() Map<String,String> kv){ return kv; }
请求:http://localhost:8080/car/2/owner/lisi/30
返回:{"id":"2","userName":"lisi","age":"30"}
获取请求头@RequestHeader
①获取指定请求头
@GetMapping("/car/{id}/owner/{userName}") public Map<String,Object> getCar(@PathVariable("id") int id, @PathVariable("userName") String userName, @RequestHeader("User-Agent") String userAgent){ Map<String,Object> map = new HashMap<>(); map.put("id",id); map.put("userName",userName); map.put("userAgent",userAgent); return map; }
请求返回:
{"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36","id":1,"userName":"zhangsan"}
②获取所有的请求头
@GetMapping("/car/{id}/owner/{userName}") public Map<String,String> getCar(@PathVariable("id") int id, @PathVariable("userName") String userName, @RequestHeader Map<String,String> heads){ return heads; }
{"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0","sec-ch-ua":"\"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":"\"Windows\"","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9","cookie":"Idea-71259e56=b05a3fec-a744-4e6b-aeb2-f38a5c6b6dd0; Webstorm-142c3cad=9dee10a5-f1c0-4ed5-97c6-4b442beeb5b3"}
标签:userName,map,常用,PathVariable,Map,sec,参数,注解,id From: https://www.cnblogs.com/ixtao/p/16727762.html