雷丰阳Springboot视频培训教程问题解决记录之四——Springboot 拦截器WebMvcConfigurerAdapter高版本的替换方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lswx2006/article/details/89305583

以下WebMvcConfigurerAdapter 比较常用的重写接口

/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

新的版本解决方案目前有两种

方案1 直接实现WebMvcConfigurer

@Configuration
public class WebMvcConfg implements WebMvcConfigurer {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/index").setViewName("index");
        }

}

推荐方案2 直接继承WebMvcConfigurationSupport

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/index").setViewName("index");
        }

}

说明:源码下WebMvcConfigurerAdapter是实现WebMvcConfigurer接口,所以直接实现WebMvcConfigurer接口也可以;WebMvcConfigurationSupport与WebMvcConfigurerAdapter、接口WebMvcConfigurer处于同一个目录下WebMvcConfigurationSupport包含WebMvcConfigurer里面的方法,由此看来版本中应该是推荐使用WebMvcConfigurationSupport类的,WebMvcConfigurationSupport应该是新版本中对WebMvcConfigurerAdapter的替换和扩展。

猜你喜欢

转载自blog.csdn.net/Lswx2006/article/details/89305583
今日推荐