【SpringBoot】使用@MatrixVariable注解,手动开启矩阵变量

SpringBoot中默认是不可以使用矩阵变量的,需要手动配置。
原因在于路径处理函数UrlPathHelper中的removeSemicolonContent变量默认为true,代表支持移除分号后的内容;

解决方案有两种:其核心都在于将removeSemicolonContent变量设置为false

1. 使用@Bean的方式向容器中添加webMvcConfigurer组件

    @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,重写configurePathMatch方法

@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        // 设置为不移除分号后面的内容
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

版权声明:本文为qq_43530796原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。