WebMvcConfigurerAdapter类型已弃用


116

我只是迁移到Spring 5.0.1.RELEASEMVC 版本,但突然在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/");
    }
  ....
  }

我该如何删除!


这列出了您可以在Spring Boot中处理静态资源的所有不同方式frugalisminds.com/spring-boot-serve-static-web-content
Sanjay-Dev

Answers:


226

从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


1
如果我super.configureMessageConverters(converters)现在该如何翻译此代码该怎么办?现在没有super参考。
tryHard18年

1
@yami您只需调用configureMessageConverters(converters),它将运行在接口上定义的默认方法
Plog

@ Plog,@ Yami:按照建议进行操作会产生java.lang.StackOverflowError,因为省略.super开始会导致递归循环,永远不会结束。
ThirstForKnowledge

2
将转换器添加到列表中,将关闭默认转换器注册。通过首先调用super.configureMessageConverters(converters),您可能想要保留默认转换器。要简单地添加转换器而不影响默认注册,请考虑改用方法extendMessageConverters(java.util.List)docs.spring.io/spring/docs/current/javadoc-api/org/…)。
ThirstForKnowledge

1
@ThirstForKnowledge哦,这很糟糕。您应该在接口上调用超默认方法的方式是:WebMvcConfigurer.super.configureMessageConverters(converters)
Plog

7

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

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

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

}

0

在春季,每个请求都将通过DispatcherServlet进行处理。为了避免通过DispatcherServlet(Front contoller)请求静态文件,我们配置了MVC静态内容

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

  • 我们已经将/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配置

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

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

spring.mvc.static-path-pattern=/resources/static/**
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.