栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringBoot重写配置的方法】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringBoot重写配置的方法】

文章目录
  • 不用@EnableWebMvc注解,使用 @Configuration + WebMvcConfigurer 自定义规则
    • 方法一:实现WebMvcConfigurer接口
    • 方法二:容器中放一个WebMvcConfigurer
  • 重写addFormatters配置函数

新版默认为false

不用@EnableWebMvc注解,使用 @Configuration + WebMvcConfigurer 自定义规则

自定义一个配置类实现接口WebMvcConfigurer
jdk8有接口的默认实现,我们只需要重写我们想重写的方法即可。

方法一:实现WebMvcConfigurer接口
@Configuration(proxyBeanMethods = false)
public class MyConfig implements WebMvcConfigurer{
    //@Bean
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        //UrlPathHelper
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        //不移除分号后面的内容,矩阵变量功能才能生效
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}
方法二:容器中放一个WebMvcConfigurer

@Configuration(proxyBeanMethods = false)
public class MyConfig{
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
}

重写addFormatters配置函数
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter() {
            @Override
            public Pet convert(String source) {
                if(StringUtils.hasText(source)){
                    Pet pet = new Pet();
                    String[] split = source.split(",");
                    pet.setName(split[0]);
                    pet.setAge(split[1]);
                    return pet;
                }
                return null;
            }
        });
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/865220.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号