解讀靜態(tài)資源訪問static-locations和static-path-pattern
靜態(tài)資源訪問static-locations和static-path-pattern
靜態(tài)資源配置底層源碼
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
//配置訪問地址為/webjars/**時(shí),去/META-INF/resources/webjars文件夾下尋找資源
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
}
靜態(tài)資源默認(rèn)前綴:
private String staticPathPattern = "/**";
靜態(tài)資源默認(rèn)地址:
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;靜態(tài)資源目錄

如果每個(gè)目錄下面都有相同的文件,那么訪問的優(yōu)先級(jí)為 META-INF>resources>static>public
靜態(tài)資源訪問前綴(默認(rèn)無前綴)可以使用下面的yaml內(nèi)容來設(shè)置
spring:
mvc:
static-path-pattern: /liang/** //會(huì)導(dǎo)致歡迎頁和favicon.ico失效靜態(tài)資源存放地址(靜態(tài)文件只能存放在文件夾yuan里面)
spring:
web:
resources:
static-locations: classpath:/yuan/當(dāng)配置文件如下
spring
web:
resources:
static-locations: classpath:/yuan/
mvc:
static-path-pattern: /liang/**
可以直接通過地址 http://localhost:8080/liang/a.png 直接進(jìn)行訪問,查看到想要結(jié)果
當(dāng)靜態(tài)訪問前綴為/**時(shí),靜態(tài)資源目錄下有一個(gè)a.png,controller控制層的@RequestMapping("/a.png")。

得到結(jié)果

結(jié)論:請(qǐng)求進(jìn)來,先去controller看能不能處理,不能處理的所有請(qǐng)求又都交給靜態(tài)資源處理器。靜態(tài)資源找不到就報(bào)404
為什么歡迎頁(index.html)有靜態(tài)資源訪問前綴就不能訪問了

通過 http://localhost:8080/liang/index.html可以直接訪問到界面,但是通過 http://localhost:8080/liang 或者 http://localhost:8080/ 都不能直接訪問到index。
但是如果把靜態(tài)資源訪問前綴去除,就可以通過 http://localhost:8080/ 訪問到index.html了.
這是因?yàn)榈讓幼隽颂幚?/p>

實(shí)現(xiàn)WebMvcConfigurer接口
會(huì)把自定義配置加載到默認(rèn)的配置中
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("訪問的路徑").addResourceLocations("資源的路徑");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
}配置文件中靜態(tài)資源目錄為


可以簡單理解為:實(shí)現(xiàn)WebMvcConfigurer接口,可以把自己自定義的一些配置加載到系統(tǒng)的默認(rèn)配置中
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 遞歸查詢所有子節(jié)點(diǎn)id的方法實(shí)現(xiàn)
在多層次的數(shù)據(jù)結(jié)構(gòu)中,經(jīng)常需要查詢一個(gè)節(jié)點(diǎn)下的所有子節(jié)點(diǎn),本文主要介紹了java 遞歸查詢所有子節(jié)點(diǎn)id的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Spring?Cloud?Gateway整合sentinel?實(shí)現(xiàn)流控熔斷的問題
本文給大家介紹下?spring?cloud?gateway?如何整合?sentinel實(shí)現(xiàn)流控熔斷,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧2022-02-02
SpringBoot整合MybatisPlusGernerator實(shí)現(xiàn)逆向工程
在我們寫項(xiàng)目的時(shí)候,我們時(shí)常會(huì)因?yàn)樾枰獎(jiǎng)?chuàng)建很多的項(xiàng)目結(jié)構(gòu)而頭疼,本文主要介紹了SpringBoot整合MybatisPlusGernerator實(shí)現(xiàn)逆向工程,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法
在實(shí)際項(xiàng)目使用中,必須要考慮服務(wù)的安全性,當(dāng)服務(wù)部署到互聯(lián)網(wǎng)以后,就要考慮服務(wù)被惡意請(qǐng)求和暴力攻擊的情況,所以本文給大家介紹了SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法,需要的朋友可以參考下2024-11-11

