1 spring.mvc.static-path-pattern

spring.mvc.static-path-pattern=/static/**只有静态资源的访问路径为/static/**时,才会处理请求。比如访问http://localhost:8080/static/a.css,处理方式是据模式匹配后的文件名查找本地文件。按spring.resources.static-locations指定查找的本地文件的位置。

2 spring.resources.static-locations

spring.resources.static-locations自定义Springboot前端静态资源的位置。默认Springboot将从如下位置,按优先级查找静态资源:

spring.resources.static-locations = classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources

3 配置file路径

# 反斜线,windows目录分隔符,前一个\是转义字符,后一个\是目录分隔符。
spring.resources.static-locations=file:\\d:\\dist\\
# 或spring.resources.static-locations=file:///d:/dist/

4 WebMvcConfigurer配置代替


@Configuration
public class CorsConfig implements WebMvcConfigurer {
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry
		.addResourceHandler("/static/**")
        .addResourceLocations("/file:\\d:\\dist\\/")
        .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
	}
}

参考:https://www.jianshu/p/21a7c7ccdec0.

更多推荐

spring.mvc.static-path-pattern和spring.resources.static-locations