问题:添加注解 @MatrixVariable 失败
报错信息:Resolved [org.springframework.web.bind.MissingMatrixVariableException: Missing matrix variable ‘name’ for method parameter of type String]
报错截图:![报错截图](https://i-blog.csdnimg.cn/blog_migrate/780b35139776879ec17840b2ac3acb69.png)
报错原因:SpringBoot 默认是无法使用矩阵变量绑定参数的。需要覆盖 WebMvcConfigurer 中的 configurePathMatch 方法。
解决方案:新增 WebMvcConfig 配置类,实现 WebMvcConfigurer 接口,重写 configurePathMatch 方法。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
多学一点:如果是基于XML的配置,需要加enable-matrix-variables="true"
配置
<mvc:annotation-driven enable-matrix-variables="true" />