Spring Boot靜態(tài)資源路徑的配置與修改詳解
默認(rèn)路徑
在Spring Boot 2.7.2版本中,查看默認(rèn)靜態(tài)資源路徑,在WebProperties.class
中如下
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
可以看到默認(rèn)資源路徑有4個(gè)。
使用Spring Initializr
新建Spring Boot項(xiàng)目,自帶static
目錄,直接將前端資源文件放到該目錄下,啟動(dòng)項(xiàng)目,訪問(wèn)http://localhost:端口號(hào)/資源目錄/名稱.html
即可;
例如,有一個(gè)front
目錄,該目錄下存在一個(gè)index.html
文件,將此目錄放于src/main/resources/static
下,并且未修改端口號(hào),訪問(wèn)http://localhost:8080/front/index.html
即可看到訪問(wèn)成功。
修改路徑
使用配置文件進(jìn)行修改
對(duì)于低版本,在配置文件application.yml
中如下:
spring:
resources:
static-locations: classpath:/
代表將資源目錄直接放在src/main/resources/
下
但是,對(duì)于高版本,該方式已棄用,不推薦?。?!
對(duì)于高版本,在配置文件application.yml
中如下:
spring:
web:
resources:
static-locations: classpath:/
高版本這樣設(shè)置,可以成功訪問(wèn)http://localhost:8080/front/index.html
使用配置類進(jìn)行修改
新建配置類WebMvcConfig.java
繼承WebMvcConfigurationSupport
類
package com.aiw.waimai.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 設(shè)置靜態(tài)資源映射 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("開(kāi)始進(jìn)行靜態(tài)資源映射。。。"); registry.addResourceHandler("/**").addResourceLocations("classpath:/"); } }
可以成功訪問(wèn)http://localhost:8080/front/index.html
注意:兩種配置方式不可同時(shí)存在,并且修改后默認(rèn)的訪問(wèn)路徑就失效了;對(duì)于配置類方式,@Slf4j是Lombok提供的注解,方便打印日志,非必須
【更新】網(wǎng)上看到WebMvcConfigurationSupport
已過(guò)時(shí),故更新為實(shí)現(xiàn)WebMvcConfigurer
接口
package com.aiw.waimai.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Slf4j @Configuration public class WebMvcConfig implements WebMvcConfigurer { /** * 設(shè)置靜態(tài)資源映射 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("開(kāi)始進(jìn)行靜態(tài)資源映射。。。"); registry.addResourceHandler("/**").addResourceLocations("classpath:/"); } }
到此這篇關(guān)于Spring Boot靜態(tài)資源路徑的配置與修改詳解的文章就介紹到這了,更多相關(guān)Spring Boot靜態(tài)資源路徑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分
這篇文章主要介紹了mybatis-plus QueryWrapper and or 連用并且實(shí)現(xiàn)分頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01CentOS7和8中安裝Maven3.8.4的簡(jiǎn)單步驟
maven是屬于apache的一個(gè)工具,主要是對(duì)java進(jìn)行編譯打包,解決依賴關(guān)系,下面這篇文章主要給大家介紹了關(guān)于CentOS7和8中安裝Maven3.8.4的相關(guān)資料,需要的朋友可以參考下2022-04-04SpringBoot中HttpSessionListener的簡(jiǎn)單使用方式
這篇文章主要介紹了SpringBoot中HttpSessionListener的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理
在本篇文章里小編給各位整理了關(guān)于Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題相關(guān)內(nèi)容,有興趣的朋友們跟著參考學(xué)習(xí)下。2019-07-07SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于SpringBoot與Quartz集成實(shí)現(xiàn)分布式定時(shí)任務(wù)集群的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03SpringBoot+OCR?實(shí)現(xiàn)圖片文字識(shí)別
本文主要介紹了SpringBoot+OCR 實(shí)現(xiàn)圖片文字識(shí)別,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法
這篇文章主要介紹了SpringBoot使用JDBC獲取相關(guān)的數(shù)據(jù)方法,JDBC與數(shù)據(jù)庫(kù)建立連接、發(fā)送 操作數(shù)據(jù)庫(kù)的語(yǔ)句并處理結(jié)果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03