SpringBoot中默认是不可以使用矩阵变量的,需要手动配置。
解决方案有两种
1. 使用@Bean的方式向容器中添加组件
@Bean // WebConfigurer
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
2. 让配置类实现WebMvcConfiguer
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 设置为不移除分号后面的内容
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
3. 前后端代码
3.1 前端
<a href="/cars/sell;low=34;brand=byd,audi,yd">/cars/sell;low=34;brand=byd,audi,yd</a><br>
3.2 java
// /cars/sell;low=34;brand=byd,audi,yd
// SpringBoot默认禁用掉了矩阵变量的功能,需要手动开启
// 矩阵变量需要保存在路径变量中{}
@ResponseBody
@GetMapping("/cars/{path}")
public Map<String, Object> carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brands,
@PathVariable String path) {
Map<String, Object> map = new HashMap<>();
map.put("low", low);
map.put("brands", brands);
map.put("path", path);
return map;
}
本文介绍如何在SpringBoot中启用矩阵变量功能,并提供两种配置方式:通过@Bean注册WebMvcConfigurer组件和实现WebMvcConfigurer接口。此外,还展示了前端链接及对应的Java控制器示例。
565

被折叠的 条评论
为什么被折叠?



