首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不推荐使用WebMvcConfigurerAdapter类型

不推荐使用WebMvcConfigurerAdapter类型
EN

Stack Overflow用户
提问于 2017-11-29 12:25:04
回答 4查看 129K关注 0票数 130

我只是迁移到spring版本的5.0.1.RELEASE,但突然在eclipse中,STS WebMvcConfigurerAdapter被标记为不推荐使用。

代码语言:javascript
运行
复制
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/");
    }
  ....
  }

我怎么能移除这个!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-11-29 13:05:41

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

代码语言:javascript
运行
复制
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

票数 264
EN

Stack Overflow用户

发布于 2018-07-26 19:35:30

我现在一直在开发名为Springfox的Swagger等效文档库,我发现在Spring5.0.8(目前正在运行)中,接口WebMvcConfigurer已经由WebMvcConfigurationSupport类实现,我们可以直接扩展该类。

代码语言:javascript
运行
复制
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

public class WebConfig extends WebMvcConfigurationSupport { }

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

代码语言:javascript
运行
复制
@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/");
}
票数 11
EN

Stack Overflow用户

发布于 2018-08-09 09:06:10

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

Spring3.1..引入了ResourceHandlerRegistry来配置ResourceHttpRequestHandlers,用于服务来自类路径、WAR或文件系统的静态资源。我们可以在web上下文配置类中以编程方式配置ResourceHandlerRegistry。

  • 我们添加了`/js/** pattern to the ResourceHandler, lets include thefoo.jsresource located in the **webapp/js/`**目录
  • 我们添加了`/resources/static/** pattern to the ResourceHandler, lets include thefoo.htmlresource located in the **webapp/resources/`**目录
代码语言:javascript
运行
复制
@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配置

代码语言:javascript
运行
复制
<mvc:annotation-driven />
  <mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/"
                 cache-period="60"/>

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

代码语言:javascript
运行
复制
spring.mvc.static-path-pattern=/resources/static/**
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47552835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档