Spring Boot 静态资源映射

默认的静态资源目录

Spring Boot 中,默认的静态资源目录如下:

优先级(数字越小优先级越高)类路径位置(在/src/main/resources/中对应的目录说明
1classpath:/META-INF/resources/常用于 WebJars,放在/META-INF/resources/下的文件会被直接映射。
2classpath:/resources//src/main/resources/resources/对应,适合放置不想放在staticpublic的资源。
3classpath:/static/最常用的静态资源目录,放在/src/main/resources/static/下的文件会映射到根路径(/**)。
4classpath:/public/static类似,放在/src/main/resources/public/下的文件同样可直接访问。
5file:./src/main/webapp/(ServletContext 根目录)/src/main/webapp/(传统 WAR 结构)下的文件。

除了最后一个,其它路径在 Spring Boot 3.x 中,由org.springframework.boot.autoconfigure.web.WebProperties.Resources.CLASSPATH_RESOURCE_LOCATIONS定义。


访问示例

文件实际路径访问 URL
/src/main/resources/META-INF/resources/index.htmlhttp://localhost:8080/index.html
/src/main/resources/META-INF/resources/1/index.htmlhttp://localhost:8080/1/index.html
/src/main/resources/resources/index.htmlhttp://localhost:8080/index.html
/src/main/resources/resources/2/index.htmlhttp://localhost:8080/2/index.html
/src/main/resources/static/index.htmlhttp://localhost:8080/index.html
/src/main/resources/static/3/index.htmlhttp://localhost:8080/3/index.html
/src/main/resources/public/index.htmlhttp://localhost:8080/index.html
/src/main/resources/public/4/index.htmlhttp://localhost:8080/4/index.html
/src/main/webapp/index.htmlhttp://localhost:8080/index.html
/src/main/webapp/5/index.htmlhttp://localhost:8080/5/index.html

相关说明

  1. 不需要在 URL 中填写静态资源目录(如/META-INF/resources//resources//static//public//webapp/),Spring Boot 会自动把根路径映射到这些目录;

  2. 优先级:如果同名资源出现在多个目录,按/META-INF/resources/ -> /resources/ -> /static/ -> /public/ -> /webapp/的顺序决定最终返回的文件;

  3. 自定义:如果想改动默认位置,可在application.properties(或application.ymlapplication.yaml)中配置spring.web.resources.static-locations,该配置会覆盖默认值,也就是上述默认的静态资源目录将失效(由于 Spring Boot 的兜底机制,部分目录依然有效,具体说明见5),需要在该配置项显式配置,优先级也由该配置的顺序决定(越靠前优先级越高);

  4. 由于 Spring Boot 的兜底机制,即便spring.web.resources.static-locations置空,或配置了自定义路径,没有配置上述的默认静态资源目录,/webapp//META-INF/resources/依旧有效(其它默认配置会失效),相当于会自动在该配置最后依次添加没有显式配置的/webapp//META-INF/resources/(注意:此时/webapp//META-INF/resources/的优先级反过来了),可通过配置spring.web.resources.add-mappings=false关闭兜底机制;

  5. 默认优先级和添加自定义路径后的配置等价于如下配置:

# 默认配置等价于如下配置(未关闭兜底机制)
spring.web.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:./src/main/webapp/
# 自定义配置等价于如下配置(未关闭兜底机制)
spring.web.resources.static-locations=classpath:/custom/,file:./src/main/webapp/,classpath:/META-INF/resources/
  1. 欢迎页(index.html):放在上述任意目录的根路径下,会被自动识别为默认首页。

默认配置相关代码

org.springframework.boot.autoconfigure.web.WebProperties.Resources.CLASSPATH_RESOURCE_LOCATIONS相关源码如下:

@ConfigurationProperties("spring.web")
public class WebProperties {
    // ...
    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 Chain chain;
        private final Cache cache;

        public Resources() {
            this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
            this.addMappings = true;
            this.customized = false;
            this.chain = new Chain();
            this.cache = new Cache();
        }

        public String[] getStaticLocations() {
            return this.staticLocations;
        }

        public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = this.appendSlashIfNecessary(staticLocations);
            this.customized = true;
        }
        // ...
    }
    // ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值