天天看點

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 畢竟單繼承多實作,而且實作接口也是官方推介的做法。是以推介方法一。