当前位置:   article > 正文

SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法_org.springframework.web.servlet.config.annotation.

org.springframework.web.servlet.config.annotation.webmvcconfigureradapter

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

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

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

  1. package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
  2. import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  7. /**
  8. * @author
  9. */
  10. @Configuration
  11. public class WebAppConfig extends WebMvcConfigurerAdapter{
  12. @Autowired
  13. private UserIDHandlerInterceptor userIDHandlerInterceptor;
  14. /**
  15. * addPathPatterns 添加拦截规则
  16. * excludePathPatterns 排除拦截规则
  17. *
  18. * @param registry
  19. */
  20. @Override
  21. public void addInterceptors(InterceptorRegistry registry) {
  22. registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
  23. }
  24. }

 

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

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package org.springframework.web.servlet.config.annotation;
  6. import java.util.List;
  7. import org.springframework.format.FormatterRegistry;
  8. import org.springframework.http.converter.HttpMessageConverter;
  9. import org.springframework.lang.Nullable;
  10. import org.springframework.validation.MessageCodesResolver;
  11. import org.springframework.validation.Validator;
  12. import org.springframework.web.method.support.HandlerMethodArgumentResolver;
  13. import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
  14. import org.springframework.web.servlet.HandlerExceptionResolver;
  15. /** @deprecated */
  16. @Deprecated
  17. public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
  18. public WebMvcConfigurerAdapter() {
  19. }
  20. public void configurePathMatch(PathMatchConfigurer configurer) {
  21. }
  22. public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
  23. }
  24. public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
  25. }
  26. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  27. }
  28. public void addFormatters(FormatterRegistry registry) {
  29. }
  30. public void addInterceptors(InterceptorRegistry registry) {
  31. }
  32. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  33. }
  34. public void addCorsMappings(CorsRegistry registry) {
  35. }
  36. public void addViewControllers(ViewControllerRegistry registry) {
  37. }
  38. public void configureViewResolvers(ViewResolverRegistry registry) {
  39. }
  40. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  41. }
  42. public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
  43. }
  44. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  45. }
  46. public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  47. }
  48. public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  49. }
  50. public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
  51. }
  52. @Nullable
  53. public Validator getValidator() {
  54. return null;
  55. }
  56. @Nullable
  57. public MessageCodesResolver getMessageCodesResolver() {
  58. return null;
  59. }
  60. }

 

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

 

 

方法一(官方推介):

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

  1. package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
  2. import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  7. /**
  8. * @author
  9. */
  10. @Configuration
  11. public class WebAppConfig implements WebMvcConfigurer{
  12. @Autowired
  13. private UserIDHandlerInterceptor userIDHandlerInterceptor;
  14. /**
  15. * addPathPatterns 添加拦截规则
  16. * excludePathPatterns 排除拦截规则
  17. *
  18. * @param registry
  19. */
  20. @Override
  21. public void addInterceptors(InterceptorRegistry registry) {
  22. registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
  23. }

 

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

继承 WebMvcConfigurationSupport,代码如下:

  1. package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
  2. import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  7. /**
  8. * @author
  9. */
  10. @Configuration
  11. public class WebAppConfig extends WebMvcConfigurationSupport{
  12. @Autowired
  13. private UserIDHandlerInterceptor userIDHandlerInterceptor;
  14. /**
  15. * addPathPatterns 添加拦截规则
  16. * excludePathPatterns 排除拦截规则
  17. *
  18. * @param registry
  19. */
  20. @Override
  21. public void addInterceptors(InterceptorRegistry registry) {
  22. registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
  23. super.addInterceptors(registry);
  24. }
  25. }

PS:Java 毕竟单继承多实现,而且实现接口也是官方推介的做法。所以推介方法一。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/576344
推荐阅读
相关标签
  

闽ICP备14008679号