Java 不推荐使用 WebMvcConfigurerAdapter 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47552835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:39:05  来源:igfitidea点击:

The type WebMvcConfigurerAdapter is deprecated

javaspringspring-mvc

提问by alveomaster

I just migrate to spring mvc version 5.0.1.RELEASEbut suddenly in eclipse STS WebMvcConfigurerAdapter is marked as deprecated

我只是迁移到 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/");
    }
  ....
  }

How can i remove this!

我怎么能去掉这个!

采纳答案by Plog

Since Spring 5 you just need to implement the interface WebMvcConfigurer:

从 Spring 5 开始,您只需要实现接口WebMvcConfigurer

public class MvcConfig implements WebMvcConfigurer {

This is because Java 8 introduced default methods on interfaces which cover the functionality of the WebMvcConfigurerAdapterclass

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

See here:

看这里:

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

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

回答by Aniruddha Tekade

I have been working on Swagger equivalent documentation library called Springfoxnowadays and I found that in the Spring 5.0.8 (running at present), interface WebMvcConfigurerhas been implemented by class WebMvcConfigurationSupportclass which we can directly extend.

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

import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

public class WebConfig extends WebMvcConfigurationSupport { }

And this is how I have used it for setting my resource handling mechanism as follows -

这就是我如何使用它来设置我的资源处理机制如下 -

@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/");
}

回答by Yash

In Spring every request will go through the DispatcherServlet. To avoid Static file request through DispatcherServlet(Front contoller) we configure MVC Static content.

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

Spring 3.1.introduced the ResourceHandlerRegistry to configure ResourceHttpRequestHandlers for serving static resources from the classpath, the WAR, or the file system. We can configure the ResourceHandlerRegistry programmatically inside our web context configuration class.

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

  • we have added the /js/**pattern to the ResourceHandler, lets include the foo.jsresource located in the webapp/js/directory
  • we have added the /resources/static/**pattern to the ResourceHandler, lets include the foo.htmlresource located in the webapp/resources/directory
  • 我们已将/js/**模式添加到 ResourceHandler,让我们包含foo.js位于webapp/js/目录中的资源
  • 我们已将/resources/static/**模式添加到 ResourceHandler,让我们包含foo.html位于webapp/resources/目录中的资源
@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 Configuration

XML 配置

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

Spring Boot MVC Static Contentif the file is located in the WAR's webapp/resourcesfolder.

如果文件位于 WAR 的webapp/resources文件夹中,则为Spring Boot MVC 静态内容

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

回答by Do Nhu Vy

Use org.springframework.web.servlet.config.annotation.WebMvcConfigurer

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

With Spring Boot 2.1.4.RELEASE (Spring Framework 5.1.6.RELEASE), do like this

使用 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();
    }

}