1、版本要求
Spring boot1.x的版本可以用 WebMvcConfigurerAdapter,但是此接口在Spring boot2.x的版本已经标为过期,因此对于2.x版本的情况需要用WebMvcConfigurationSupport或者WebMvcConfigurer来实现。下面分别进行介绍:
2、Spring boot2.x实现方式
方法一、创建类WebMvcConfigurer,继承WebMvcConfigurationSupport类进行实现,代码如下:
package com.test.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
//配置文件路径
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//默认的资源映射需要填写,不然不能正常访问
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//配置外部资源目录的映射,/image目录为前端访问的路径,后面配置静态资源的绝对路径
registry.addResourceHandler("/image/**").addResourceLocations("file:"+uploadFolder);
//调用基类的方法
super.addResourceHandlers(registry);
}
}
在application.properties配置文件中增加映射路径配置:
#windows系统配置路径
#file.uploadFolder=C:/Users/Administrator/Desktop/pic/
#linux系统配置路径
file.uploadFolder=/root/pic/
坑坑坑,重点:
1.默认的资源映射,不能缺少,不然默认的static目录将不能访问!
2.配置"/image/“路径时,我第一次配置成”/image*/",但是我static目录恰巧有个资源目录叫做images,这时导致配置失败,默认路径和外部资源配置功能都不能正常使用,因此,配置外部路径时一定不能和工程内部的目录有重名,或者有能适配上的名字。
3. 一个项目中:只能存在一个 WebMvcConfigurationSupport。不能存在多个,请切记!!!
配置完毕后就可以运行程序进行测试了:http://localhost:8080/image/xxx.jpg
方法二、创建类WebMvcConfigurer,实现WebMvcConfigurer接口类进行实现,代码如下:
package com.test.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfigurer implements WebMvcConfigurer {
//配置文件路径
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//默认的资源映射需要填写,不然不能正常访问
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//配置外部资源目录的映射,/image目录为前端访问的路径,后面配置静态资源的绝对路径
registry.addResourceHandler("/image/**").addResourceLocations("file:"+uploadFolder);
}
}
测试方法同上。
3、Spring boot1.x实现方式
创建类WebMvcConfigurer,继承WebMvcConfigurerAdapter 类进行实现,代码如下:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
//配置文件路径
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//默认的资源映射需要填写,不然不能正常访问
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//配置外部资源目录的映射,/image目录为前端访问的路径,后面配置静态资源的绝对路径
registry.addResourceHandler("/image/**").addResourceLocations("file:"+uploadFolder);
}
}
配置文件和测试方法同上。