一种 WebMvcConfigurerAdapter 它已经过时了

我刚刚迁移 spring mvc 版本
5.0.1.RELEASE

, 但突然b。 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 5 您只需实现一个接口
WebMvcConfigurer

:


public class MvcConfig implements WebMvcConfigurer {


这是因为 Java 8 在涵盖类的功能的接口上输入了默认方法
WebMvcConfigurerAdapter


看这里:

https://docs.spring.io/spring/ ... .html

小明明

赞同来自:

我工作了 Swagger 等效文档库
Springfox

目前发现了 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/"/;
}

石油百科

赞同来自:

使用
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//;
}

}

詹大官人

赞同来自:

在 Spring 每个请求都将通过

DispatcherServlet

. 通过避免静态文件请求 DispatcherServlet /Front contoller/, 我们是曲调
https://www.baeldung.com/sprin ... urces
.

Spring 3.1.

介绍 ResourceHandlerRegistry 用于设置 ResourceHttpRequestHandlers 维护静态资源 classpath, WAR 或文件系统。 我们可以定制 ResourceHandlerRegistry 我们的Web上下文配置类中的软件。

我们添加了模板

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


https://docs.spring.io/spring- ... ntent
, 如果文件在该文件夹中 WAR

webapp/resources

.


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

要回复问题请先登录注册