SpringBoot中WebMvcConfigurerAdapter、WebMvcConfigurationSupport以及WebMvcConfigurer

在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js、css等)。

@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }
    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源;  *.css , *.js
                //SpringBoot已经做好了静态资源映射
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html","/","/user/login");
            }
        };
        return adapter;
    }
    @Bean
    public LocaleResolver localeResolver(){
 
        return new MyLocaleResolver();
    }
 
}

在spring boot2.0+以后,WebMvcConfigurerAdapter就过时了,目前通过继承WebMvcConfigurationSupport类(ps:继承后好像MVC自动配置不生效了)或者实现WebMvcConfigurer接口来扩展springMVC的功能。然而该版本自定义的拦截器会拦截静态资源,因此在使用spring2.0+时,配置拦截器之后,我们要把静态资源的路径加入到不拦截的路径之中。

@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
 
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
 
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/index.html").setViewName("login");
            registry.addViewController("/main.html").setViewName("dashboard");
        }
 
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //将静态资源排除在拦截之外
            registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                    .excludePathPatterns("/index.html","/","/user/login","/static/**");
        }
        @Bean
        public LocaleResolver localeResolver(){
            return new MyLocaleResolver();
        }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WebMvcConfigurer接口主要用于配置Spring MVC的行为。在Spring Boot 2.0之前,我们可以通过重写WebMvcConfigurerAdapter类的方法来添加自定义拦截器和消息转换器等。然而,在Spring Boot 2.0之后,WebMvcConfigurerAdapter类被标记为@Deprecated(弃用)。所以,为了实现自定义配置,官方推荐直接实现WebMvcConfigurer接口或继承WebMvcConfigurationSupport类。 如果我们想自定义消息转换器,可以使用ContentNegotiationConfigurer。ContentNegotiationConfigurer能够配置内容协商策略,例如选择哪种媒体类型(如JSON或XML)来作为响应的内容格式。我们可以通过自定义ContentNegotiationConfigurer来注册自己的消息转换器。 另外,如果我们对Spring默认的URL解析方式不满意,我们可以通过配置PathMatchConfigurer来进行自定义。PathMatchConfigurer可以配置路径匹配的规则,例如是否允许后缀模式、是否允许尾部斜杠等。如果不配置这些选项,Spring会使用默认配置。具体的配置方式可以参考RequestMappingHandlerMapping和ContentNegotiationStrategy配置。 综上所述,WebMvcConfigurer接口依赖于ContentNegotiationConfigurer和PathMatchConfigurer来实现自定义配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBootWebMvcConfigurer配置接口的详解](https://blog.csdn.net/pan_junbiao/article/details/120039885)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringMVC源码系列之自定义WebMvcConfigurer](https://blog.csdn.net/Crabime/article/details/98207697)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值