WebMvcConfigurerAdapter 类型已经被弃用。

134

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

我该如何移除这个!

4个回答

269

1
如果我有 super.configureMessageConverters(converters),现在该如何翻译这段代码?现在没有 super 可以参考了。 - tryingHard
1
@yami 你只需要调用 configureMessageConverters(converters) 方法,它会运行在接口中定义的默认方法。 - Plog
@Plog,@Yami:按照建议操作会产生java.lang.StackOverflowError,因为省略.super会启动一个递归的无限调用循环。 - ThirstForKnowledge
3
在将转换器添加到列表中时,会关闭默认的转换器注册。通过首先调用super.configureMessageConverters(converters),您可能希望保留默认的转换器。如果想要简单地添加转换器而不影响默认注册,请考虑使用方法extendMessageConverters(java.util.List)(https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurer.html#extendMessageConverters-java.util.List-)。 - ThirstForKnowledge
@ThirstForKnowledge 是的,因为这个原因我刚刚删除了这行代码。我不想让程序进入无限循环。 - tryingHard
1
@ThirstForKnowledge 噢,这是我的错。在接口上调用超级默认方法的方式应该是:WebMvcConfigurer.super.configureMessageConverters(converters)。 - Plog

11

最近我一直在开发一个名为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/");
}

2
在Spring中,每个请求都会通过DispatcherServlet。为了避免静态文件请求通过DispatcherServlet(前端控制器),我们配置MVC Static contentSpring 3.1.引入了ResourceHandlerRegistry来配置ResourceHttpRequestHandlers,以从类路径、WAR或文件系统提供静态资源。我们可以在Web上下文配置类中以编程方式配置ResourceHandlerRegistry。
  • 我们已将/js/**模式添加到ResourceHandler中,让我们包含位于webapp/js/目录中的foo.js资源
  • 我们已将/resources/static/**模式添加到ResourceHandler中,让我们包含位于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 静态内容,如果文件位于 WAR 文件的 webapp/resources 文件夹中。

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

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接