首先配置
创建一个config类并且实现WebMvcConfigurer接口,然后重写configurePathMatch方法:
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
接口
之后写接口:
@RequestMapping(value = "userMsg/{path}")
public Map getUserMsg(@MatrixVariable("name") String userName,
@MatrixVariable("age") int userAge,
@MatrixVariable("pet") List<String> pet,
@PathVariable("path") String path){
Map<String,Object> map = new HashMap<>();
map.put("name",userName);
map.put("age",userAge);
map.put("pet",pet);
map.put("path",path);
System.out.println(userName+" "+userAge+" "+pet);
return map;
}
写好后运行并且访问url:
http://localhost:9999/userMsg/msg;name=java;age=22;pet=dog,cat,bird
返回结果:
{“path”:“msg”,“name”:“java”,“age”:22,“pet”:“dog,cat,bird”}。
总结
矩阵变量可以用在session被禁用的时候,我们仍然能获取用户数据。