天天看点

SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法

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 毕竟单继承多实现,而且实现接口也是官方推介的做法。所以推介方法一。