Register Spring HandlerInterceptor Without WebMvcConfigurationSupport

前端 未结 2 2012
清酒与你
清酒与你 2021-01-11 20:15

I\'m trying to register an instance of HandlerInterceptor in Spring using Java Config without extending WebMvcConfigurationSupport. I\'m c

2条回答
  •  日久生厌
    2021-01-11 21:14

    Just stating @Blauhirn's comment, WebMvcConfigurerAdapter is deprecated as of version 5.0:

    Deprecated as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter

    Refer to the new way to do it:

    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new MyCustomInterceptor())
            // Optional
            .addPathPatterns("/myendpoint");
        }
    }
    

    Plus, as stated here, do not annotate this with @EnableWebMvc, if you want to keep Spring Boot auto configuration for MVC.

提交回复
热议问题