自定义mvc配置--(配置UrlPathHelper&Converter)

1.新建WebConfig类继承WebMvcConfigurer接口

// proxyBeanMethods = true 或不写,是Full模式,proxyBeanMethods = false 是lite模式
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {

2.新建WebMvcConfigurer组件

 @Bean
   public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {

3.重写配置方法

@Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper(); // 导入系统设置
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }

4.也可在WebMvcConfigurer组件外重新定义组件或方法

// 配置UrlPathHelper,使Url中的;后的内容不被移除,从而启用矩阵变量
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer){
        UrlPathHelper urlPathHelper = new UrlPathHelper(); // 导入系统设置
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

完整示例:

// proxyBeanMethods = true 或不写,是Full模式,proxyBeanMethods = false 是lite模式
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
    /*
    // 配置UrlPathHelper,使Url中的;后的内容不被移除,从而启用矩阵变量
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer){
        UrlPathHelper urlPathHelper = new UrlPathHelper(); // 导入系统设置
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
    */

    // 配置UrlPathHelper,使Url中的;后的内容不被移除,从而启用矩阵变量
    @Bean
   public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper(); // 导入系统设置
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }

            @Override
            public void addFormatters(FormatterRegistry registry){
                // 添加转换器 (From,to)
                registry.addConverter(new Converter<String, Pet>() {
                    // 转换方法并返回指定类型对象
                    @Override
                    public Pet convert(String source) {
                        //string,integer
                        if(!StringUtils.isEmpty(source)){
                            Pet pet = new Pet();
                            String [] split = source.split(",");
                            pet.setAge(Integer.parseInt(split[1]));
                            pet.setName(split[0]);
                            return pet;
                        }
                        return null;
                    }

                });
            }
        };
   }


    // 改变默认的_method
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();// 导入系统设置
        methodFilter.setMethodParam("_m");// 绑定属性值
        return methodFilter;
    }
}


posted @ 2021-08-20 13:22  潮哥啊边伦个  阅读(360)  评论(0)    收藏  举报