17.开启矩阵变量的两种方式
//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; }
要使用矩阵变量 需要开启矩阵变量,默认是不开始的
矩阵变量的规则,使用restful路径变量才能解析矩阵变量,比如cars{sell 路径变量};low=30;brand=byd,yd矩阵变量,以K,V的形式
如果有多个路径变量 就 cars{sell};low=30;brand=byd,yd/{dealer};address=xxx
cars/sell;low=30;brand=byd,yd/dealer;address=xxx 为了方便自己理解才写成上面的伪代码,这个是正规写法
开启方式一,以实现接口的方式开启,默认中springboot中是实现了这个接口的,但是默认实现是不开启矩阵变量的
@Configuration(proxyBeanMethods = false) public class WebConfig implements WebMvcConfigurer { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); // 设置为不移除分号后面的内容 urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }
开启方式二,以组件的方式开启,因为它就是个组件
@Bean // WebConfigurer public WebMvcConfigurer webMvcConfigurer() { return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; }
浙公网安备 33010602011771号