1. 配置spring.mvc.static-path-pattern之后,进行访问
spring.mvc.static-path-pattern=/static/**只有静态资源的访问路径为/static/**时,才会处理请求。比如访问http://localhost:8080/static/a.css
2.配置文件访问路径:
spring.resources.static-locations:file:///D:/hx/
配置之后,重启服务就可以直接通过上面的请求去访问到D:\hx文件夹中的静态文件
备选:
直接在WebMvcConfigurer实现类中加入
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceHandler请求路径
//addResourceLocations 在项目中的资源路径
//setCacheControl 设置静态资源缓存时间
registry
.addResourceHandler("/static/**")
.addResourceLocations("/file:\\d:\\hx\\/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
}
}