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
方法
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 不移除;后面的内容。矩阵变量功能就可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}