解讀靜態(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內(nèi)容來設(shè)置
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 直接進行訪問,查看到想要結(jié)果
當靜態(tài)訪問前綴為/**時,靜態(tài)資源目錄下有一個a.png,controller控制層的@RequestMapping("/a.png")。

得到結(jié)果

結(jié)論:請求進來,先去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)的默認配置中
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis Plus插件機制與執(zhí)行流程原理分析詳解
這篇文章主要介紹了MyBatis Plus插件機制與執(zhí)行流程原理分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
詳解spring cloud config整合gitlab搭建分布式的配置中心
這篇文章主要介紹了詳解spring cloud config整合gitlab搭建分布式的配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA設(shè)置代碼的快捷編輯模板Live Templates,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Java中如何動態(tài)創(chuàng)建接口的實現(xiàn)方法
這篇文章主要介紹了Java中如何動態(tài)創(chuàng)建接口的實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-09-09
springboot如何使用yml文件方式配置shardingsphere
這篇文章主要介紹了springboot如何使用yml文件方式配置shardingsphere問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

