Spring Boot 配置静态资源获取路径

Spring Boot About 2,848 words

配置源码

@ConfigurationProperties("spring.web")
public class WebProperties {

    public static class Resources {

        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
                "classpath:/resources/", "classpath:/static/", "classpath:/public/" };

        /**
         * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
         * /resources/, /static/, /public/].
         */
        private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

        public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = appendSlashIfNecessary(staticLocations);
            this.customized = true;
        }

        private String[] appendSlashIfNecessary(String[] staticLocations) {
            String[] normalized = new String[staticLocations.length];
            for (int i = 0; i < staticLocations.length; i++) {
                String location = staticLocations[i];
                normalized[i] = location.endsWith("/") ? location : location + "/";
            }
            return normalized;
        }
    }

}

优先级

默认优先级

可以从代码中看到,默认的静态资源获取的优先级是定义在WebProperties.Resources中的CLASSPATH_RESOURCE_LOCATIONS数组中。

classpath:/META-INF/resources/ 
classpath:/resources/
classpath:/static/ 
classpath:/public/

修改优先级

配置文件中设置静态资源路径,可以按需求调整,但只有填写的路径才会生效。(如未填写classpath:/static/,则只有前三个生效)

spring:
  web:
    resources:
      static-locations: classpath:/public/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/

外部文件

需要添加机器上其他目录作为读取的文件夹。

使用file:表示外部文件夹,这里的assets表示工程执行目录下的assets/文件夹,源码中appendSlashIfNecessary方法会自动添加/

注意:下面的配置只有assets文件生效,上诉4classpath路径因为没有配置在这,则不会生效。

spring:
  web:
    resources:
      static-locations: file:assets

设置路径

如果路径设置的是file:assetsassets文件夹下有一个a.txt,则访问路径就是:http://localhost:8080/mydir/a.txt

spring:
  mvc:
    static-path-pattern: /mydir/**

代码设置

可以设置多个文件夹。

@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
            .addResourceLocations("file:" + System.getProperty("user.dir") + "/assets/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
        registry.addResourceHandler("/**")
            .addResourceLocations("file:" + System.getProperty("user.dir") + "/123/");
        registry.addResourceHandler("/DirOne/**")
            .addResourceLocations("file:" + System.getProperty("user.dir") + "/456/");
        registry.addResourceHandler("/DirTwo/**")
            .addResourceLocations("file:" + System.getProperty("user.dir") + "/789/");
    }
}

备注

同时配置了WebMvcConfigureraddResourceHandlers和配置文件的spring.web.resources.static-locations,以WebMvcConfigurer为准。即spring.web.resources.static-locations不生效。

Views: 1,475 · Posted: 2023-02-14

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh