SpringBoot学习笔记(三) 矩阵变量 @MatrixVariable 注解使用和理解
SpringBoot学习笔记(三) 矩阵变量 @MatrixVariable 注解使用和理解
SpringBoot @MatrixVariable 注解的用法
示例一
假设前端的请求路径为
/cars/sell;low=34;brand=byd,aodi,yd
那么我们Controller应该这样接收参数
@GetMapping("/cars/{path}")public Map carsSell(@MatrixVariable("low") Integer low,@MatrixVariable("brand") List<String> brand,@PathVariable("path") String path){......}
示例二
前端请求路径为
/boss/1;age=20/2;age=10
那么我们Controller应该这样接收参数
@GetMapping("/boss/{bossId}/{empId}")public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,@MatrixVariable(value = "age",pathVar = "empId") Integer empAge){......}
SpringBoot默认禁用了矩阵变量的功能。要想使用必须手动开启。
开启方式:在配置类上实现 WebMvcConfigurer 接口,并重写 configurePathMatch 方法
@Overridepublic void configurePathMatch(PathMatchConfigurer configurer) {UrlPathHelper urlPathHelper = new UrlPathHelper();// 不移除;后面的内容。矩阵变量功能就可以生效urlPathHelper.setRemoveSemicolonContent(false);configurer.setUrlPathHelper(urlPathHelper);}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
