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
The type WebMvcConfigurerAdapter is deprecated
提问by alveomaster
I just migrate to spring mvc version 5.0.1.RELEASE
but 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 WebMvcConfigurerAdapter
class
这是因为 Java 8 在接口上引入了默认方法,这些方法涵盖了WebMvcConfigurerAdapter
类的功能
See here:
看这里:
回答by Aniruddha Tekade
I have been working on Swagger equivalent documentation library called Springfox
nowadays and I found that in the Spring 5.0.8 (running at present), interface WebMvcConfigurer
has been implemented by class WebMvcConfigurationSupport
class 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 thefoo.js
resource located in thewebapp/js/
directory- we have added the
/resources/static/**
pattern to the ResourceHandler, lets include thefoo.html
resource located in thewebapp/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();
}
}