SpringBoot2 静态资源访问问题

朴灿烈づ我的快乐病毒、 2023-02-14 06:55 157阅读 0赞

1. 静态资源默认位置

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

调整顺序会调整优先级

默认静态资源访问路径
spring.mvc.static-path-pattern=/**

也就是说如果不覆盖默认配置SpringBoot可以很好的工作。

注意:

  1. 使用 @EnableWebMvc 注解会完全覆盖默认行为。
  2. 继承WebMvcConfigurationSupport配置类也会覆盖默认行为。

将不会提供默认的静态资源访问方式,需要自己覆盖,也就是说不能自动设置默认首页,不能自动处理静态路径匹配规则

解决方法:

  1. @Configuration
  2. public class MyWebConfig extends WebMvcConfigurationSupport {
  3. @Value("${spring.mvc.static-path-pattern}")
  4. private String staticPathPattern;
  5. @Value("${spring.resources.static-locations}")
  6. private String staticLocations;
  7. @Override
  8. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  9. registry.addResourceHandler(staticPathPattern,"/**").addResourceLocations(StringUtils.split(staticLocations, ","));
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,157人围观)

还没有评论,来说两句吧...

相关阅读