-path-pattern和-locations

时间:2025-03-20 09:44:55

1 .static-path-pattern

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

2 -locations

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

-locations = classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
  • 1

3 配置file路径

# 反斜线,windows目录分隔符,前一个\是转义字符,后一个\是目录分隔符。
-locations=file:\\d:\\dist\\
# 或-locations=file:///d:/dist/
  • 1
  • 2
  • 3

4 WebMvcConfigurer配置代替


@Configuration
public class CorsConfig implements WebMvcConfigurer {
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry
		.addResourceHandler("/static/**")
        .addResourceLocations("/file:\\d:\\dist\\/")
        .setCacheControl((1, ).cachePublic());
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

参考:/p/21a7c7ccdec0.