Spring Boot静态资源访问和配置全解析

1.SpringBoot静态资源映射规则

Springboot源码,在WebMvcAutoConfiguration类中,有如下代码:

 public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
                }
               //此处做静态资源文件夹映射
                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl).setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
                }

            }
        }

1.1 静态资源映射类

在staticPathPattern中,使用了ResourceProperties,然后在ResourceProperties类中继承了Resources:
在这里插入图片描述
在Resources类中,设置了常量CLASSPATH_RESOURCE_LOCATIONS:

public static class Resources {
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
        private String[] staticLocations;
        private boolean addMappings;
        private boolean customized;
        private final WebProperties.Resources.Chain chain;
        private final WebProperties.Resources.Cache cache;

Spring Boot为我们提供的默认静态资源映射,它会自动到"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"寻找静态资源。

在这几个默认的地址下,可以直接访问静态资源:http://localhost:8080/jquery-3.5.1.min.js
Spring Boot 默认会挨个从 public、resources和static 里面找是否存在相应的资源,如果有则直接返回。

2.自定义静态资源映射类

我们可以发现,这就是Spring Boot为我们提供的默认静态资源映射,如果想要自定义映射规则的话,继承WebMvcConfigurer即可。

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将/static/**访问映射到classpath:/mystatic/
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/mystatic/");
    }
}

这时候我们在main/resorces下创建mystatic文件夹,将自己的静态资源放入,启动Springboot,访问
http://localhost:8080/static/jquery-3.5.1.min.js(静态资源名)
http://localhost:8080/mystatic/jquery-3.5.1.min.js这个路径是访问不通的。

2.1在配置文件中进行设置

application.properties或者application.yml都可以,这里以application.properties为例。
配置:

// 配置静态资源访问前缀
spring.mvc.static-path-pattern=/mystatic/**

这表示只有静态资源的访问路径为/mystatic/**时,才会处理请求,

# 配置静态资源访问前缀
spring.mvc.static-path-pattern=/mystatic/**
# 配置静态资源路径,默认配置失效
spring.web.resources.static-locations[0]=classpath:/mystatic

但当访问resources和static里静态资源时,就会404报错,访问不到了,这个是因为配置文件中如果进行了静态资源路径的配置,那么默认的配置就失效了。
两篇文章结合看:
Spring Boot配置静态资源的地址与访问路径(spring.mvc.static-path-pattern和spring.web.resources.static-locations)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值