SpringBoot中的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則詳解
1. 靜態(tài)資源
查看源碼如下:
package org.springframework.boot.autoconfigure.web.servlet;
......省略部分......
public class WebMvcAutoConfiguration {
......省略部分......
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
// 將靜態(tài)資源映射,都添加到映射中心
this.addResourceHandler(registry, this.mvcProperties.getWebjarsPathPattern(), "classpath:/META-INF/resources/webjars/");
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
registration.addResourceLocations(new Resource[]{resource});
}
});
}
}
......省略部分......
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, Consumer<ResourceHandlerRegistration> customizer) {
if (!registry.hasMappingForPattern(pattern)) {
ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{pattern});
customizer.accept(registration);
registration.setCachePeriod(this.getSeconds(this.resourceProperties.getCache().getPeriod()));
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
this.customizeResourceHandlerRegistration(registration);
}
}
......省略部分......
}1.1 默認靜態(tài)資源
默認的靜態(tài)資源目錄為:resources/META-INF/resources、resources/public、resources/resources、resources/static。一般用于存放圖片、視頻、css、js文件
默認的訪問靜態(tài)資源的URL根路徑為:/**
所以訪問resources/META-INF/resources/baidu1.jpg的URL為http://localhost:8080/baidu1.jpg
1.2 Controller高優(yōu)先級
如果一個Controller的的@RequestMapping定義的訪問路徑,和靜態(tài)資源的URL訪問路徑一樣,則返回Controller的內(nèi)容
1.3 修改靜態(tài)資源的URL根路徑
可以通過配置參數(shù): spring.mvc.static-path-pattern: /static/**,修改靜態(tài)資源的URL根路徑。此時訪問resources/META-INF/resources/baidu1.jpg的URL為http://localhost:8080/static/baidu1.jpg
1.4 修改靜態(tài)資源的目錄
可以通過配置參數(shù):spring.web.resources.static-locations: [classpath:/my-resources/],修改靜態(tài)資源的目錄。會使resources/public、resources/resources、resources/static目錄失效,但resources/META-INF/resources目錄不失效
1.5 訪問webjars依賴包的靜態(tài)資源
會自動將resources/META-INF/resources/webjars/**,作為靜態(tài)資源目錄
這里我們以jquery為例,進行講解。在pom.xml添加如下依賴
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.1</version>
</dependency>org.webjars.jquery的jar包的目錄結(jié)構(gòu)如下:

然后訪問http://localhost:8080/static/webjars/jquery/3.6.1/jquery.js。顯示如下:

可以使用spring.mvc.webjars-path-pattern=/wj/**修改webjars路徑訪問前綴,這樣需要通過http://localhost:8080/static/wj/jquery/3.6.1/jquery.js進行訪問
1.6 靜態(tài)資源的關(guān)閉
可以通過配置參數(shù):spring.web.resources.add-mappings=false,關(guān)閉所有靜態(tài)資源
1.7 靜態(tài)資源在瀏覽器的緩存
- 瀏覽器會將靜態(tài)資源緩存,在緩存時間過期前,不會向服務(wù)器進行靜態(tài)資源的請求。通過配置參數(shù)進行設(shè)置:spring.web.resources.cache.period=3,單位為秒,默認為0秒
- cacheControl: HTTP緩存控制。參考: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching。通過
spring.web.resources.cache.cachecontrol.max-age=7200設(shè)置,默認是7200秒,優(yōu)先級比spring.web.resources.cache.period高。如果沒手動設(shè)置該參數(shù),但手動設(shè)置了spring.web.resources.cache.period,則period參數(shù)生效 - useLastModified:是否使用最后一次修改。配合cacheControl使用。比如如果瀏覽器訪問了一個靜態(tài)資源index.js,如果服務(wù)器這個資源沒有發(fā)生變化,下次訪問的時候就可以直接讓瀏覽器用自己緩存中的東西,而不用給服務(wù)器發(fā)請求,此時瀏覽器狀態(tài)碼為304。通過
spring.web.resources.cache.use-last-modified=true設(shè)置,默認為true - 共享緩存和私有緩存。共享緩存: 客戶瀏覽器和代理服務(wù)器都會緩存;私有緩存: 只有客戶瀏覽器會緩存。通過
spring.web.resources.cache.cachecontrol.cache-public=true和spring.web.resources.cache.cachecontrol.cache-private=false設(shè)置。默認由瀏覽器自行決定,但一般是私有緩存
1.8 靜態(tài)資源實戰(zhàn)
Controller中定義了一個@RequestMapping(“/static/baidu1.jpg”)路徑,和resources\META-INF\resources\baidu1.jpg靜態(tài)資源的路徑一樣。內(nèi)容如下:
package com.hh.springboottest.myController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/static/baidu1.jpg")
public String helloHandle() {
return "hello springboot";
}
}resources目錄內(nèi)容如下。分別有resources/META-INF/baidu1.jpg、resources/my-resources/baidu2.jpg、resources/public/baidu3.jpg、resources/resources/baidu4.jpg、resources/static/baidu5.jpg

application.yaml內(nèi)容如下。分別定義了靜態(tài)資源訪問URL根路徑,和靜態(tài)資源目錄
spring:
mvc:
static-path-pattern: /static/**
web:
resources:
static-locations: [classpath:/my-resources/]訪問http://localhost:8080/static/baidu1.jpg,效果如下:

訪問http://localhost:8080/static/baidu2.jpg,效果如下:

http://localhost:8080/static/baidu3.jpg、http://localhost:8080/static/baidu4.jpg、http://localhost:8080/static/baidu5.jpg不能訪問
1.9 通過代碼自定義靜態(tài)資源訪問規(guī)則
如下。添加一個靜態(tài)資源訪問URL,并指定其訪問資源路徑和緩存時間。其原理是給容器中添加一個WebMvcConfigurer組件,讓配置的底層行為都生效
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.concurrent.TimeUnit;
// @EnableWebMvc // 禁用springBoot的默認配置
// 這是一個配置類。給容器中放一個WebMvcConfigurer組件,就能自定義底層。有以下兩種方式
// 1. 可以讓配置類實現(xiàn)WebMvcConfigurer接口
// 2. 可以在配置類中,將@Bean注解標注在方法上,然后方法返回WebMvcConfigurer
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 默認就會保留以前規(guī)則
// WebMvcConfigurer.super.addResourceHandlers(registry);
// 自己添加新的規(guī)則
registry.addResourceHandler("/my-static/**")
.addResourceLocations("classpath:/static1/", "classpath:/static2/")
.setCacheControl(CacheControl.maxAge(1180, TimeUnit.SECONDS));
}
}到此這篇關(guān)于SpringBoot的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則的文章就介紹到這了,更多相關(guān)SpringBoot static靜態(tài)資源訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis-plus與mybatis共存的實現(xiàn)
本文主要介紹了mybatis-plus與mybatis共存的實現(xiàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Java?windows環(huán)境構(gòu)建圖文教程
這篇文章主要為大家介紹了Java?windows環(huán)境構(gòu)建圖文教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-12-12
Java中的字節(jié),字符輸出流與字節(jié)和字符輸入流的簡單理解
這篇文章主要介紹了java 字節(jié)流和字符流的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-07-07
基于Restful接口調(diào)用方法總結(jié)(超詳細)
下面小編就為大家?guī)硪黄赗estful接口調(diào)用方法總結(jié)(超詳細)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Zuul實現(xiàn)動態(tài)路由與權(quán)限過濾器方式
文章介紹如何通過Zuul實現(xiàn)動態(tài)路由與權(quán)限驗證,利用自定義過濾器和配置刷新機制,提升系統(tǒng)靈活性與安全性,確保接口訪問可控且無需重啟2025-07-07
Springboot項目配置阿里云SSL證書的實現(xiàn)步驟
本文詳細介紹了SpringBoot項目中配置HTTPS,包括證書申請、下載及安裝過程,及設(shè)置HTTP自動重定向至HTTPS,確保網(wǎng)站數(shù)據(jù)傳輸?shù)陌踩?感興趣的可以了解一下2025-08-08

