Springboot访问静态资源

版本

	<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默认的静态资源路径有四个:

  1. /META-INF/resources/
  2. /resources/
  3. /static/
  4. /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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值