首页 > 编程语言 > 详细

Spring boot 静态资源访问

时间:2019-11-23 15:22:51      阅读:73      评论:0      收藏:0      [点我收藏+]

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\/"
      ]
    }

技术分享图片

自定义静态资源地址

例子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/

Spring boot 静态资源访问

原文:https://www.cnblogs.com/yang21/p/11918097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!