springboot注解
注解:
@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@CookieValue、@RequestBody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | @RestController public class ParameterTestController { // car/2/owner/zhangsan @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; } //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( "bossAge" ,bossAge); map.put( "empAge" ,empAge); return map; } } |
本文作者:durtime
本文链接:https://www.cnblogs.com/durtime/p/14103893.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2020-11-17 每日日报