SpringMVC的@MatrixVariable注解
1. 背景
@MatrixVariable注解与矩阵变量有关
一般我们会定义这样的url:/cars/{path}/?a=xxx&b=XXX,这是使用查询字符串的方式提交参数,在后台方法我们使用@RequestParam注解来获取参数。另外,我们考虑这样的url:/cars/sell;a=xxx;b=xxx,XXX,这就是使用矩阵变量的方式提交参数,那么我们就使用@MatrixVariable注解
一般,矩阵变量会应用到这样的场景。在页面开发中,如果Cookie被禁用了,该如何获得Session中的数据?
如果使用Cookie,通过获取Cookie中的JSessionID,找到Session对象,从而获取该Session中的数据。而Cookie被禁用了,就无法获取JSessionID,也就不能用这种方法获取Session的数据。这时,我们可以使用url重写的方式解决这一问题,比如这样的url:/abc;jsessionid=xxxxx,也就是将Cookie的值使用矩阵变量的方式传递
2. 矩阵变量的格式
一般以矩阵变量的方式传递参数,则url有多种格式
/cars/sell;low=34;brand=bmw;brand=audi;brand=benz这是在;后面以key=value形式加上参数,之间用;分开/cars/sell;low=34;brand=bmw,audi,benz这是将相同key的value值以,分开/boss/1;age=20/2;age=18这是以/boss/{bossId}/{empId}的形式,并且在bossId和empId中分别带上了参数
矩阵变量是绑定在路径变量中的。比如,对于/cars/sell;low=34;brand=bmw,audi,benz这个url,在Controller方法上的@RequestMapping中的url为/cars/{path}
3. 开启SpringBoot对矩阵变量的支持
在SpringBoot中,需要手动开启对矩阵变量的支持。在url路径的处理上,SpringMVC使用UrlPathHelper进行解析,这个类中有个变量removeSemicolonContent默认为true,表示默认移除url中的分号内容。所以为了支持矩阵变量,需要自定义SpringMVC,有两种解决方法
方法一:
我们在配置类中放入一个WebMvcConfigurer,标注@bean注解,将这个类放入容器中,在这个类中覆盖configurePathMatch方法
@Configuration(proxyBeanMethods = false)
public class MyConfig{
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}
方法二:
直接将配置类实现WebMvcConfigurer,且在配置类中实现configurePathMatch方法
@Configuration(proxyBeanMethods = false)
public class MyConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
这样就开启了SpringBoot对于矩阵变量的支持
4. @MatrixVariable的使用
那么,对于相应的Controller方法,我们可以这样写
/**
* /cars/sell;low=34;brand=bmw,audi,benz
* 或者/cars/sell;low=34;brand=bmw;brand=audi;brand=benz
*/
@GetMapping("/cars/{path}")
public Map<String, Object> test3(@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;
}
当我们发起这样的请求时/cars/sell;low=34;brand=bmw,audi,benz或者/cars/sell;low=34;brand=bmw;brand=audi;brand=benz,页面显示的结果为{"path":"sell","low":34,"brand":["bmw","audi","benz"]}
另外,对于这样的url:/boss/1;age=20/2;age=18,在url中有两个名称相同的矩阵变量,相应的处理方法为
/**
* /boss/1;age=20/2;age=18
*/
@GetMapping("/boss/{bossId}/{empId}")
public Map<String, Object> test4(@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;
}
在@MatrixVariable注解中有pathVar属性,这个属性来区分是哪个路径下的变量
这样当我们发起这样的请求时 /boss/1;age=20/2;age=18,显示的结果为{"bossAge":20,"empAge":18}
5. 总结
-
SpringBoot默认不支持矩阵变量,需要自定义SpringMVC的
WebMvcConfigurer中的UrlPathHelper来开启 -
矩阵变量必须要有url路径变量才能被解析

浙公网安备 33010602011771号