在SpringBoot项目中,默认有5个位置可以用来存放静态资源,分别为:
1、classpath:/META-INF/resources/
2、classpath:/resources/
3、classpath:/static/
4、classpath:/public/
5、/
源码解析
WebMvcAutoConfiguration
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), this.resourceProperties.getStaticLocations());
}
}
this.mvcProperties.getStaticPathPattern()对应 /
this.resourceProperties.getStaticLocations()对应下面
自定义配置,可以通过application.properties定义,也可以通过java代码定义
1.application.properties定义
2.java代码