SpringBoot静态资源映射

目录

[12 静态资源映射](#12 静态资源映射)

[12.1 源码](#12.1 源码)

[12.2 静态资源路径映射规则](#12.2 静态资源路径映射规则)

[12.2.1 WebJars 映射规则(/webjars/** 路径)](#12.2.1 WebJars 映射规则(/webjars/** 路径))

[12.2.2 静态资源文件夹映射规则(/** 路径)](#12.2.2 静态资源文件夹映射规则(/** 路径))

[12.2.3 欢迎页规则](#12.2.3 欢迎页规则)


12 静态资源映射

12.1 源码

注:代码是Springboot2.0.1的代码

java 复制代码
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)public class ResourceProperties implements ResourceLoaderAware
{//可以设置和静态资源有关的参数,缓存时间等
java 复制代码
WebMvcAuotConfiguration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
     if (!this.resourceProperties.isAddMappings()) {
           logger.debug("Default resource handling disabled");
           return;
      } 
    Integer cachePeriod = this.resourceProperties.getCachePeriod();
    if (!registry.hasMappingForPattern("/webjars/**")) {
         customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META‐INF/resources/webjars/").setCachePeriod(cachePeriod));
    } 
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    //静态资源文件夹映射
    if (!registry.hasMappingForPattern(staticPathPattern)) {
          customizeResourceHandlerRegistration(registry.
    addResourceHandler(staticPathPattern).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
    }
    } /
    /配置欢迎页映射
    @Bean
    public WelcomePageHandlerMapping welcomePageHandlerMapping(
           ResourceProperties resourceProperties) {
           return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
    this.mvcProperties.getStaticPathPattern());
    }
    //配置喜欢的图标
    @Configuration
    @ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
    public static class FaviconConfiguration {
           private final ResourceProperties resourceProperties;
           public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
    }
     @Bean
    public SimpleUrlHandlerMapping faviconHandlerMapping() {
           SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
           mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
           //所有 **/favicon.ico
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
    faviconRequestHandler());
    return mapping;
    } 
    @Bean
    public ResourceHttpRequestHandler faviconRequestHandler() {
           ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
           requestHandler.setLocations(this.resourceProperties.getFaviconLocations());
           return requestHandler;
    }
}

12.2 静态资源路径映射规则

12.2.1 WebJars 映射规则(/webjars/** 路径)

WebJars 会将前端静态资源(如 jQuery、Bootstrap 等)打包成 Jar 包,让 Java 项目可以通过 Maven 依赖引入。Spring Boot 对 WebJars 的映射规则是:所有 /webjars/** 请求,都会去 classpath:/META-INF/resources/webjars/ 下(如下图)找资源。

在 pom.xml 中添加 jQuery 的 WebJars 依赖:

java 复制代码
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

引入依赖后,启动 Spring Boot 项目后,在浏览器中访问: http://localhost:8080/webjars/jquery/3.3.1/jquery.js
如果能成功打开 jQuery 的源码文件,说明 WebJars 映射规则生效 ------Spring Boot 自动找到了 classpath:/META-INF/resources/webjars/jquery/3.3.1/jquery.js 这个资源。

12.2.2 静态资源文件夹映射规则(/** 路径)

  1. Spring Boot 对普通静态资源(如自定义的 js、css、html 等)的映射规则是:所有 /** 请求,会依次去以下静态资源文件夹找资源(优先级从左到右递减):
  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/
    注:classpath指的是src/main/resources/
  1. 验证
    在 src/main/resources 下创建不同的静态资源文件夹,并分别放入同名文件 test.txt,内容区分开(方便验证优先级):
  • src/main/resources/META-INF/resources/test.txt:内容写 META-INF/resources
  • src/main/resources/resources/test.txt:内容写 resources
  • src/main/resources/static/test.txt:内容写 static
  • src/main/resources/public/test.txt:内容写 public
    步骤 2:访问测试文件
    启动项目后,在浏览器中访问: http://localhost:8080/test.txt
    最终会看到内容为 META-INF/resources 的文件 ------ 这验证了静态资源文件夹的优先级顺序(META-INF/resources 优先级最高)。如果按优先级从高到低,依次删除文件,即可验证所有的优先级。
    步骤 3:验证自定义静态资源访问
    再创建一个自定义的静态资源,比如在 src/main/resources/static/ 下创建 myjs.js,内容为 hellooooooo。访问 http://localhost:8080/myjs.js,如果能看到源码,说明 /** 映射规则生效,Spring Boot 从 static 文件夹中找到了该资源。

12.2.3 欢迎页规则

Spring Boot 的欢迎页规则是:访问 localhost:8080/ 时,会自动找静态资源文件夹下的 index.html。
优先级同12.2.2。
验证步骤同12.2.2。
启动项目后,直接访问 http://localhost:8080/,如果能看到index.html页面,说明欢迎页规则生效 ------Spring Boot 自动找到了index.html。

相关推荐
2302_1112 分钟前
java依赖小结
java·运维·数据库
sunshine22 girl5 分钟前
Java学习三 高级语法3, 方法
java·学习
captain37634 分钟前
网络原理(3)-TCP核心机制连接管理▲▲▲
java·服务器·网络·ide·网络协议·tcp/ip·java-ee
企业数字化笔记1 小时前
固定资产历史数据怎么导入系统?Excel模板、字段映射和数据校验
android·java·数据库·后端
troy1281 小时前
Python 基础语法(一):变量、数据类型与运算符
java·服务器·python
还是奇怪1 小时前
OpenAI GPT-5.6 构建指南拆解:创业公司如何用模型选择与 Responses API 降低 Agent 成本
java·数据库·人工智能·gpt
可涵不会debug1 小时前
LangChain 文本分割器详解:字符分割、Token 分割、硬约束递归分割实战
java·前端·数据库
sbjdhjd1 小时前
ThinkPHP 5.0.10 缓存写入型 RCE 复盘:从手动搭建、换行绕过到源码单步验证 | 05
java·后端·安全·spring·网络安全·数据挖掘·php
程序员清风1 小时前
记忆系统设计:让 Agent 拥有短期记忆与长期记忆
java·服务器·网络
EatFan2 小时前
前后端分离项目中 Token 到底应该怎么设计?Access Token + Refresh Token 实战
java·前端·后端