Spring boot 静态资源访问

Spring Boot 默认静态资源访问

spring-configuration-metadata.json
{
      "name": "spring.resources.static-locations",
      "type": "java.lang.String[]",
      "description": "Locations of static resources. Defaults to classpath:[\/META-INF\/resources\/, \/resources\/, \/static\/, \/public\/].",
      "sourceType": "org.springframework.boot.autoconfigure.web.ResourceProperties",
      "defaultValue": [
        "classpath:\/META-INF\/resources\/",
        "classpath:\/resources\/",
        "classpath:\/static\/",
        "classpath:\/public\/"
      ]
    }

109e5a304e0089318c5398f00be1e412.png

自定义静态资源地址

例子1

请求路径:/image/**
请求文件:classpath:/images/

@Configurationpublic class ImageMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**")
                .addResourceLocations("classpath:/images/");
    }
}
例子2

请求路径:/resources/**
请求文件:/public, classpath:/static/

@Configuration@EnableWebMvcpublic class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
            .addResourceLocations("/public", "classpath:/static/")
            .setCachePeriod(31556926);
    }
}
例子3
spring:
  mvc:
    static-path-pattern: /image/**
  resources:
    static-locations: classpath:/images/

猜你喜欢

转载自www.cnblogs.com/yang21/p/11918097.html