SpringBoot2.0 是基于 spring 5.0 实现的。

在Spring 5.0 中,已经将 WebMvcConfigurerAdapter 抽象类加上 @Deprecated 注解 记为过时。

下面的代码就是过时方法:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
    }
}

我们来看看 WebMvcConfigurerAdapter  方法,上面已经加了@Deprecated 注解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
 
package org.springframework.web.servlet.config.annotation;
 
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
 
/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public WebMvcConfigurerAdapter() {
    }
 
    public void configurePathMatch(PathMatchConfigurer configurer) {
    }
 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }
 
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    }
 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    }
 
    public void addFormatters(FormatterRegistry registry) {
    }
 
    public void addInterceptors(InterceptorRegistry registry) {
    }
 
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }
 
    public void addCorsMappings(CorsRegistry registry) {
    }
 
    public void addViewControllers(ViewControllerRegistry registry) {
    }
 
    public void configureViewResolvers(ViewResolverRegistry registry) {
    }
 
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    }
 
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
    }
 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
 
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
 
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    }
 
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    }
 
    @Nullable
    public Validator getValidator() {
        return null;
    }
 
    @Nullable
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}

现在我们采用新的方法来处理:

方法一(官方推介):

我们看到,WebMvcConfigurerAdapter  抽象类实现了 WebMvcConfigurer 接口,这里我们只需要将 extends WebMvcConfigurerAdapter  替换为 implements WebMvcConfigurer 即可。代码如下:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig implements WebMvcConfigurer{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
    }
}

方法二(不推介,也过期了):

继承 WebMvcConfigurationSupport,代码如下:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
        super.addInterceptors(registry);
    }
}
PS:Java 毕竟单继承多实现,而且实现接口也是官方推介的做法。所以推介方法一。
————————————————
版权声明:本文为CSDN博主「java_代码搬运工」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38164123/article/details/80392904

SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法相关推荐

  1. Java11.0.2怎么生成JRE_java环境变量配置,jdk13.0.1中没有jre解决办法

    标签:完成后   回车   手动   完成   cmd   没有   alt   span   环境变量配置 java.Oracle中下载了最新的jdk13.0.1,安装之后发现没自动生成jre,导致 ...

  2. JavaScript中的ParseInt(08)和“09”返回0的原因分析及解决办法

    今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...

  3. linux telnet 127.0.0.1 不通,Linux 出现telnet: 127.0.0.1: Connection refused错误解决办法

    Linux 出现telnet: 127.0.0.1: Connection refused错误解决办法 Linux 出现telnet: connect to address 127.0.0.1: Co ...

  4. php7 jpgraph,JpGraph4.0.2中文乱码以及在php7.0版本无法显示的解决办法

    [摘要] PHP即"超文本预处理器",是一种通用开源脚本语言.PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言.PHP独特的语法混合了C.Java.Perl以及 ...

  5. android notifydatasetchanged 刷新错误,Android 调用notifyDataSetChanged方法失败解决办法

    Android 调用notifyDataSetChanged方法失败解决办法 如果使用ListView.GridView等进行数据展示,当绑定的数据有了更新的时候,需要实时刷新ListView,即调用 ...

  6. SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题

    SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题 参考文章: (1)SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题 (2)https://www. ...

  7. eclipse中用maven多模块管理,然后主项目无法调用其他被依赖项目里的方法,解决办法

    eclipse中用maven多模块管理,然后主项目无法调用其他被依赖项目里的方法,解决办法 参考文章: (1)eclipse中用maven多模块管理,然后主项目无法调用其他被依赖项目里的方法,解决办法 ...

  8. 关于Keil.STM32F1xx_DFP.1.0.5代码无法烧录解决办法

    ** 关于Keil.STM32F1xx_DFP.1.0.5代码无法烧录解决办法 ** 1.首先,若在点击下载按钮后弹出以下错误, 可能原因有: (1)单片机未连接. (2)单片机连接异常,接口或线路有 ...

  9. 无法访问localhost与127.0.0.1/本地服务器的解决办法

    无法访问localhost与127.0.0.1/本地服务器的解决办法 无法访问本地服务器 解决方式: 打开"程序与功能" 点击"启动或关闭Windows功能" ...

最新文章

  1. NetworkManagementService介绍
  2. php rsa数字签名为空,如何使用PHP将数字签名(RSA,证书等)添加到任何文件?
  3. python开发网页视频播放器_HTML5 VideoAPI,打造自己的Web视频播放器
  4. 《解释的工具:生活中的经济学原理》读书笔记7(完)
  5. 【开源项目】特斯拉+树莓派实现车牌识别检测系统
  6. socket用法linux,linux socket编程,要用到哪些函数,和用法介绍?_Linux_天涯问答_天涯社区...
  7. Element-UI 表单验证规则rules 配置参数说明
  8. html5液体效果,HTML5/CSS3/SVG实现的液体掉落(滑落)动画
  9. 关于安装QTP之后造成环境变量java冲突问题的解决方案
  10. 2014年全国最新企业名录免费采集下载
  11. MaterialDesign+MovePicImageView实现漂亮的登陆界面
  12. Unicode(全世界每个国家字符的唯一编码0x000000 到 0x10FFFF)与UTF-8的区别
  13. sudo_拔剑-浆糊的传说_新浪博客
  14. 计算机导论课程知识总结,计算机导论课程论文
  15. 基于ESP32的智能鱼缸系统
  16. 10 06 01 繁杂
  17. 物理机通过Xshell连接不上虚拟机的解决方案
  18. Django之models.py
  19. OpenHarmony 软总线lite 源码分析
  20. java postgresql date_javapostgresql时区总结

热门文章

  1. QT的QNdefRecord类的使用
  2. linux组群账户存放在,linux用户和群组
  3. 23-26 Python File方法、OS文件/目录方法、异常处理、内置函数
  4. Hive内置运算函数,自定义函数(UDF)和Transform
  5. linux下重启weblogic(关闭和启动)
  6. C语言文件操作函数的编写
  7. 1、打印二进制机器码,程序内存分析,大端序小端序,指针数组,数组指针,数组的三种访问方式,typedef,#if-0-#endif,求数组大小,括号表达式
  8. 线性代数行列式计算之升阶法
  9. python装饰器怎么编程_Python编程:Python装饰器入门
  10. python打开浏览器怎么写_使用python调用浏览器并打开一个网址的例子