解讀靜態(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/**時,去/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)資源默認前綴:
private String staticPathPattern = "/**";
靜態(tài)資源默認地址:
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)資源目錄

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

得到結果

結論:請求進來,先去controller看能不能處理,不能處理的所有請求又都交給靜態(tài)資源處理器。靜態(tài)資源找不到就報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了.
這是因為底層做了處理

實現(xiàn)WebMvcConfigurer接口
會把自定義配置加載到默認的配置中
@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)資源目錄為


可以簡單理解為:實現(xiàn)WebMvcConfigurer接口,可以把自己自定義的一些配置加載到系統(tǒng)的默認配置中
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java 遞歸查詢所有子節(jié)點id的方法實現(xiàn)
在多層次的數(shù)據(jù)結構中,經(jīng)常需要查詢一個節(jié)點下的所有子節(jié)點,本文主要介紹了java 遞歸查詢所有子節(jié)點id的方法實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03
Spring?Cloud?Gateway整合sentinel?實現(xiàn)流控熔斷的問題
本文給大家介紹下?spring?cloud?gateway?如何整合?sentinel實現(xiàn)流控熔斷,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友一起看看吧2022-02-02
SpringBoot整合MybatisPlusGernerator實現(xiàn)逆向工程
在我們寫項目的時候,我們時常會因為需要創(chuàng)建很多的項目結構而頭疼,本文主要介紹了SpringBoot整合MybatisPlusGernerator實現(xiàn)逆向工程,具有一定的參考價值,感興趣的可以了解一下2024-05-05

