一、矩阵变量请求格式
/users;id=1,uname=jack
二、SpringBoot开启矩阵请求
- 首先查看springboot源码关于矩阵部分的内容
- 在 WebMvcAutoConfiguration配置类中。
-
@Override public void configurePathMatch(PathMatchConfigurer configurer) { if (this.mvcProperties.getPathmatch() .getMatchingStrategy() == WebMvcProperties.MatchingStrategy.PATH_PATTERN_PARSER) { configurer.setPatternParser(pathPatternParser); } configurer.setUseSuffixPatternMatch(this.mvcProperties.getPathmatch().isUseSuffixPattern()); configurer.setUseRegisteredSuffixPatternMatch( this.mvcProperties.getPathmatch().isUseRegisteredSuffixPattern()); this.dispatcherServletPath.ifAvailable((dispatcherPath) -> { String servletUrlMapping = dispatcherPath.getServletUrlMapping(); if (servletUrlMapping.equals("/") && singleDispatcherServlet()) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setAlwaysUseFullPath(true); configurer.setUrlPathHelper(urlPathHelper); } }); } - 在此方法中的 UrlPathHelper,中对矩阵变量做了截取,代码如下:
- 往上查看变量removeSemicolonContent,默认定义为true
-
/** * Set if ";" (semicolon) content should be stripped from the request URI. * <p>Default is "true". */ public void setRemoveSemicolonContent(boolean removeSemicolonContent) { checkReadOnly(); this.removeSemicolonContent = removeSemicolonContent; }
- 开启矩阵变量
- 第一种方式。创建配置类,实现WebMvcConfigurer接口。重写 configurePathMatch() 方法
@Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); // 将默认值改为false urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); }
- 第二种方式,在SpringBoot添加容器
-
@Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; }
浙公网安备 33010602011771号