spring boot 静态资源 访问 配置

1.spring.mvc.static-path-pattern

这个配置项是告诉springboot,应该以什么样的方式去寻找资源。默认配置为 /* 。换句话说,只有静态资源满足什么样的匹配条件,Spring Boot才会处理静态资源请求

如果原先访问首页的地址是:http://localhost:8888/index.html
那么在你配置这个配置后,上面的访问就失效了,现在访问同样的页面需要这样访问:http://localhost:8888/soul/index.html
 
2.spring.resources.static-locations
这个配置项是告诉springboot去哪找资源。
# 默认值为
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

根据前后关系确定优先级

可通过配置文件配置
  mvc:
    static-path-pattern: /note/**
  resources:
    static-locations: classpath:/resources/note/

也可通过配置类注解

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/note/");
    }
}

注意: 

classpath  是指编译后classes 文件目录的相对路径
原文地址:https://www.cnblogs.com/cyh1282656849/p/12864280.html