返回介绍

静态资源的访问

发布于 2025-03-08 13:29:56 字数 4039 浏览 0 评论 0 收藏 0

发现一个神坑,Spring Boot 1.x 对静态资源(图片、js、css 代码等)有默认要求:

提供静态资源目录位置需置于 classpath(resources 目录)下,目录名需符合如下规则:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

比如说 classpath(resource 目录)下的 static 目录下有 test.png,则可直接通过 localhost:8080/test.png 访问
以上对于 Spring Boot 2.x 没用了,但是网上还是一堆复制粘贴的文章这样写,csdn 尤其是。

Spring Boot 2.x 版本实现方法:

搜到了两种解决办法,方法一没试通过,但也列在这吧

# application.properties 中设置
# 也可以是/public/**
spring.mvc.static-path-pattern=/static/**

方法二、这里说下我查到的原因,spring boot 2.x 静态资源会被 HandlerInterceptor 拦截,因为 spring boot 2.x 采用的是 spring 5.x,会对静态资源拦截

package com.springBootExample.quickStartDemo;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebMvcConfig implements WebMvcConfigurer {
  /**
   * 添加静态资源文件,外部可以直接访问地址
   *
   * @param registry
   */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
  }
}

这两函数从源码看都是可变长参数,所以可以设置多个目录来区别哪里放图片哪里放 js 代码等,如 registry.addResourceHandler("/public/**","/static/**").addResourceLocations("classpath:/public/","classpath:/static/");

  /**
   * Add a resource handler for serving static resources based on the specified URL path patterns.
   * The handler will be invoked for every incoming request that matches to one of the specified
   * path patterns.
   * <p>Patterns like {@code "/static/**"} or {@code "/css/{filename:\\w+\\.css}"} are allowed.
   * See {@link org.springframework.util.AntPathMatcher} for more details on the syntax.
   * @return a {@link ResourceHandlerRegistration} to use to further configure the
   * registered resource handler
   */
  public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {
    ResourceHandlerRegistration registration = new ResourceHandlerRegistration(pathPatterns);
    this.registrations.add(registration);
    return registration;
  }

  /**
   * Add one or more resource locations from which to serve static content.
   * Each location must point to a valid directory. Multiple locations may
   * be specified as a comma-separated list, and the locations will be checked
   * for a given resource in the order specified.
   * <p>For example, {{@code "/"}, {@code "classpath:/META-INF/public-web-resources/"}}
   * allows resources to be served both from the web application root and
   * from any JAR on the classpath that contains a
   * {@code /META-INF/public-web-resources/} directory, with resources in the
   * web application root taking precedence.
   * <p>For {@link org.springframework.core.io.UrlResource URL-based resources}
   * (e.g. files, HTTP URLs, etc) this method supports a special prefix to
   * indicate the charset associated with the URL so that relative paths
   * appended to it can be encoded correctly, e.g.
   * {@code [charset=Windows-31J]https://example.org/path}.
   * @return the same {@link ResourceHandlerRegistration} instance, for
   * chained method invocation
   */
  public ResourceHandlerRegistration addResourceLocations(String... resourceLocations) {
    this.locationValues.addAll(Arrays.asList(resourceLocations));
    return this;
  }

参考:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文