常用参数注解
路径变量@PathVariable
①获取指定路径变量:
1 2 3 4 5 6 7 8 9 | @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"}
②获取所有路径变量:
1 2 3 4 5 6 | //使用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
①获取指定请求头
1 2 3 4 5 6 7 8 9 10 | @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"}
②获取所有的请求头
1 2 3 4 5 6 7 | @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"}
获取请求参数@RequestParam
①获取指定的参数
1 2 3 4 5 6 7 8 9 | @GetMapping ( "/userInfo" ) public Map<String,Object> getUser( @RequestParam ( "name" ) String name, @RequestParam ( "hobbies" ) List<String> hobbies){ Map<String,Object> map = new HashMap<>(); map.put( "name" ,name); map.put( "hobbies" ,hobbies); return map; } |
获取cookie的值@CookieValue
①方式一
1 2 3 4 5 6 7 | @GetMapping ( "/userInfo" ) public Map<String,Object> getUser( @CookieValue ( "Webstorm-142c3cad" ) String p){ Map<String,Object> map = new HashMap<>(); map.put( "Webstorm" ,p); return map; } |
请求:http://localhost:8080/userInfo?name=xiaoming
返回:{"Webstorm":"9dee10a5-f1c0-4ed5-97c6-4b442beeb5b3"}
②方式二:声明为Cookie类型
1 2 3 4 5 6 | @GetMapping ( "/userInfo" ) public String getUser( @CookieValue ( "Idea-71259e56" ) Cookie cookie){ System.out.println(cookie.getName()+ ":" +cookie.getValue()); return "1" ; } |
获取请求体@RequestBody(post请求)
1 2 3 4 5 6 | public Map<String,Object> getUser( @RequestBody String content){ Map<String,Object> map = new HashMap<>(); map.put( "content" ,content); return map; } |
返回字符串类型
1 2 3 4 | @PostMapping ( "/userInfo" ) public String getUser( @RequestBody String content){ return content; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2020-09-25 controller
2020-09-25 启动程序