The type WebMvcConfigurerAdapter is deprecated
我只是迁移到Spring MVC版本
1 2 3 4 5 6 7 8 9 | 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 5开始,您只需要实现接口
1 | public class MvcConfig implements WebMvcConfigurer { |
这是因为Java 8在覆盖
看这里:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html
如今,我一直在开发名为
1 2 3 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; public class WebConfig extends WebMvcConfigurationSupport { } |
这就是我用来设置资源处理机制的方式,如下所示:
1 2 3 4 5 6 7 8 | @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/"); } |
使用
使用Spring Boot 2.1.4.RELEASE(Spring Framework 5.1.6.RELEASE)时,请像这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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(); } } |
在春季,每个请求都将通过DispatcherServlet。为了避免通过DispatcherServlet(Front contoller)请求静态文件,我们配置了MVC静态内容。
春天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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @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配置
1 2 3 | <mvc:annotation-driven /> <mvc:resources mapping="/staticFiles/path/**" location="/staticFilesFolder/js/" cache-period="60"/> |
如果文件位于WAR的webapp / resources文件夹中,则为Spring Boot MVC静态内容。
1 | spring.mvc.static-path-pattern=/resources/static/** |