版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
默认静态资源路径
在没有任何配置的情况下,Springboot默认的静态资源路径有四个:
- /META-INF/resources/
- /resources/
- /static/
- /public/
如果四个目录同时存在,且拥有相同的文件时,读取顺序为1>2>3>4。
访问localhost:8080/2.png测试。
自定义静态资源配置
1. 配置类
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**")
.addResourceLocations("classpath:/images/");
}
}
2. 配置文件
spring.mvc.static-path-pattern=/image/** #访问模式,Path pattern used for static resources.
spring.resources.static-locations=classpath:/images/ #资源路径,多个之间用逗号分隔,当指定了此项之后,默认的资源目录则不生效,Locations of static resources. Defaults to classpath:[/META-INF/resources/, /resources/, /static/, /public/].
访问localhost:8080/image/2.png测试
- 当同时使用配置文件和配置类时,只有配置类方式生效
开启热部署,方便调试
https://blog.csdn.net/biubiu2it/article/details/102749779