【问题标题】:The type WebMvcConfigurerAdapter is deprecated不推荐使用 WebMvcConfigurerAdapter 类型
【发布时间】:2018-05-13 03:57:46
【问题描述】:

我刚刚迁移到 spring mvc 版本 5.0.1.RELEASE 但突然在 eclipse STS WebMvcConfigurerAdapter 被标记为已弃用

public class MvcConfig extends WebMvcConfigurerAdapter {
  @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        // to serve static .html pages...
        registry.addResourceHandler("/static/**").addResourceLocations("/resources/static/");
    }
  ....
  }

我怎样才能删除这个!

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    使用org.springframework.web.servlet.config.annotation.WebMvcConfigurer

    使用 Spring Boot 2.1.4.RELEASE (Spring Framework 5.1.6.RELEASE),这样做

    package vn.bkit;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.ViewResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; // Deprecated.
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @Configuration
    @EnableWebMvc
    public class MvcConfiguration implements WebMvcConfigurer {
    
        @Bean
        public ViewResolver getViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/");
            resolver.setSuffix(".html");
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      在 Spring 中,每个请求都将通过 DispatcherServlet。为了避免通过 DispatcherServlet(Front contoller) 请求静态文件,我们配置了MVC Static content

      Spring 3.1. 引入了 ResourceHandlerRegistry 来配置 ResourceHttpRequestHandlers 以提供来自类路径、WAR 或文件系统的静态资源。我们可以在 Web 上下文配置类中以编程方式配置 ResourceHandlerRegistry。

      • 我们在 ResourceHandler 中添加了 /js/** 模式,让我们在 webapp/js/ 目录中包含 foo.js 资源
      • 我们在 ResourceHandler 中添加了 /resources/static/** 模式,让我们在 webapp/resources/ 目录中包含 foo.html 资源
      @Configuration
      @EnableWebMvc
      public class StaticResourceConfiguration implements WebMvcConfigurer {
      
          @Override
          public void addResourceHandlers(ResourceHandlerRegistry registry) {
              System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
              registry.addResourceHandler("/resources/static/**")
                      .addResourceLocations("/resources/");
      
              registry
                  .addResourceHandler("/js/**")
                  .addResourceLocations("/js/")
                  .setCachePeriod(3600)
                  .resourceChain(true)
                  .addResolver(new GzipResourceResolver())
                  .addResolver(new PathResourceResolver());
          }
      }
      

      XML 配置

      <mvc:annotation-driven />
        <mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/"
                       cache-period="60"/>
      

      Spring Boot MVC Static Content 如果文件位于 WAR 的 webapp/resources 文件夹中。

      spring.mvc.static-path-pattern=/resources/static/**
      

      【讨论】:

        【解决方案3】:

        我现在一直在研究名为 Springfox 的 Swagger 等效文档库,我发现在 Spring 5.0.8(目前正在运行)中,接口 WebMvcConfigurer 已由类 WebMvcConfigurationSupport 类实现,我们可以直接延长。

        import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
        
        public class WebConfig extends WebMvcConfigurationSupport { }
        

        这就是我使用它来设置我的资源处理机制的方式 -

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
        
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        

        【讨论】:

          【解决方案4】:

          从 Spring 5 开始你只需要实现接口WebMvcConfigurer:

          public class MvcConfig implements WebMvcConfigurer {
          

          这是因为 Java 8 在接口上引入了默认方法,涵盖了 WebMvcConfigurerAdapter 类的功能

          看这里:

          https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html

          【讨论】:

          • 如果我有super.configureMessageConverters(converters),现在我该如何翻译这段代码?现在没有 super 可以参考了。
          • @yami 你只需调用 configureMessageConverters(converters) 这将运行接口上定义的默认方法
          • @Plog, @Yami:按照建议进行操作会产生 java.lang.StackOverflowError,因为省略 .super 会启动递归的、永无止境的调用循环。
          • 将转换器添加到列表中,关闭默认转换器注册。通过首先调用 super.configureMessageConverters(converters) 您可能希望保留默认转换器。要在不影响默认注册的情况下简单地添加转换器,请考虑改用 extendMessageConverters(java.util.List) (docs.spring.io/spring/docs/current/javadoc-api/org/…) 方法。
          • @ThirstForKnowledge 哦,这是我的错。您应该在接口上调用超级默认方法的方式是:WebMvcConfigurer.super.configureMessageConverters(converters)
          猜你喜欢
          • 1970-01-01
          • 2022-08-09
          • 2015-01-08
          • 2019-02-07
          • 2011-06-22
          • 1970-01-01
          • 1970-01-01
          • 2018-08-26
          • 2023-03-09
          相关资源
          最近更新 更多