Spring Boot - Spring Boot 静态资源映射(默认静态资源映射、自定义静态资源映射)

一、静态资源映射

  1. 在 Spring Boot 中,静态资源的映射是指将特定的 URL 路径与静态资源关联起来

  2. 静态资源有例如,HTML、CSS、JS、图片等

  3. 这使得客户端可以通过 URL 路径访问这些资源


二、默认静态资源映射

概述
  • Spring Boot 默认会将以下目录中的文件映射为静态资源
  1. classpath:/static/

  2. classpath:/public/

  3. classpath:/resources/

  4. classpath:/META-INF/resources/

1、classpath:/static/
  1. 路径:src/main/resources/static/

  2. 用途:用于存放静态资源文件,例如,CSS、JavaScript、图片等

# 示例

URL 路径:http://localhost:8080/image.jpg

文件路径:src/main/resources/static/image.jpg
2、classpath:/public/
  1. 路径:src/main/resources/public/

  2. 用途:用于存放公开的静态资源文件,例如,HTML、图片等

# 示例

URL 路径:http://localhost:8080/index.html

文件路径:src/main/resources/public/index.html
3、classpath:/resources/
  1. 路径:src/main/resources/resources/

  2. 用途:用于存放静态资源文件,与 classpath:/static/ 类似

# 示例

URL 路径:http://localhost:8080/resources/style.css

文件路径:src/main/resources/resources/style.css
4、classpath:/META-INF/resources/
  1. 路径:src/main/resources/META-INF/resources/

  2. 用途:用于存放需要打包到 JAR 文件中的静态资源文件,通常用于第三方库或插件

# 示例

URL 路径:http://localhost:8080/plugin.js

文件路径:src/main/resources/META-INF/resources/plugin.js
访问优先级
  • Spring Boot 会按照以下顺序查找静态资源文件,优先级从高到低
  1. classpath:/META-INF/resources/

  2. classpath:/resources/

  3. classpath:/static/

  4. classpath:/public/

注意事项
  1. 如果自定义了静态资源路径,即 spring.mvc.static-path-pattern,默认路径仍然有效

  2. 如果需要完全覆盖默认的静态资源路径,可以在配置中指定 spring.web.resources.static-locations


三、自定义静态资源映射

1、配置文件配置
  • application.yaml 文件中,配置如下内容
spring:
  mvc:
    static-path-pattern: /static/**
  web:
    resources:
      static-locations: classpath:/custom-static/
# 如果使用的是 properties 文件

spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/custom-static/
  1. spring.mvc.static-path-pattern 是静态资源的 URL 路径

  2. spring.web.resources.static-locations 是静态资源的实际位置

# 示例

URL 路径:http://localhost:8080/static/image.jpg

文件路径:src/main/resources/custom-static/image.jpg
2、代码配置
  • 实现 WebMvcConfigurer 接口,通过代码配置可以更灵活地定义静态资源的映射,
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/download/**")
                .addResourceLocations("classpath:/file/");
    }
}
  1. addResourceHandler 方法是静态资源的 URL 路径

  2. addResourceLocations 方法是静态资源的实际位置

# 示例

URL 路径:http://localhost:8080/file/download/image.jpg

文件路径:src/main/resources/file/image.jpg
访问优先级
  1. 如果同时使用了配置文件配置与代码配置,Spring Boot 会合并两者的配置

  2. 如果路径冲突,代码配置会优先级更高

映射到文件系统
  1. 配置文件配置
spring:
  mvc:
    static-path-pattern: /file/download/**
  web:
    resources:
      static-locations: file:D:/fileUpload/
  1. 代码配置
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/download/**")
                .addResourceLocations("file:D:/fileUpload/");
    }
}

你可能感兴趣的:(Java,-,简化库与框架编程,spring,boot,后端,java,java-ee,spring,intellij-idea,intellij,idea)