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 默認(rèn)靜態(tài)資源
默認(rèn)的靜態(tài)資源目錄為:resources/META-INF/resources、resources/public、resources/resources、resources/static。一般用于存放圖片、視頻、css、js文件
默認(rèn)的訪問靜態(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根路徑。此時(shí)訪問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為例,進(jìn)行講解。在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進(jìn)行訪問
1.6 靜態(tài)資源的關(guān)閉
可以通過配置參數(shù):spring.web.resources.add-mappings=false,關(guān)閉所有靜態(tài)資源
1.7 靜態(tài)資源在瀏覽器的緩存
- 瀏覽器會將靜態(tài)資源緩存,在緩存時(shí)間過期前,不會向服務(wù)器進(jìn)行靜態(tài)資源的請求。通過配置參數(shù)進(jìn)行設(shè)置:spring.web.resources.cache.period=3,單位為秒,默認(rèn)為0秒
- cacheControl: HTTP緩存控制。參考: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Caching。通過
spring.web.resources.cache.cachecontrol.max-age=7200
設(shè)置,默認(rèn)是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ā)生變化,下次訪問的時(shí)候就可以直接讓瀏覽器用自己緩存中的東西,而不用給服務(wù)器發(fā)請求,此時(shí)瀏覽器狀態(tài)碼為304。通過
spring.web.resources.cache.use-last-modified=true
設(shè)置,默認(rèn)為true - 共享緩存和私有緩存。共享緩存: 客戶瀏覽器和代理服務(wù)器都會緩存;私有緩存: 只有客戶瀏覽器會緩存。通過
spring.web.resources.cache.cachecontrol.cache-public=true
和spring.web.resources.cache.cachecontrol.cache-private=false
設(shè)置。默認(rèn)由瀏覽器自行決定,但一般是私有緩存
1.8 靜態(tài)資源實(shí)戰(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,并指定其訪問資源路徑和緩存時(shí)間。其原理是給容器中添加一個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的默認(rèn)配置 // 這是一個配置類。給容器中放一個WebMvcConfigurer組件,就能自定義底層。有以下兩種方式 // 1. 可以讓配置類實(shí)現(xiàn)WebMvcConfigurer接口 // 2. 可以在配置類中,將@Bean注解標(biāo)注在方法上,然后方法返回WebMvcConfigurer @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 默認(rèn)就會保留以前規(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)文章
Java File類 mkdir 不能創(chuàng)建多層目錄的解決
這篇文章主要介紹了Java File類 mkdir 不能創(chuàng)建多層目錄的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09datax-web在windows環(huán)境idea中模塊化打包部署操作步驟
這篇文章主要介紹了datax-web在windows環(huán)境idea中模塊化打包部署操作步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Java 按照字節(jié)來截取字符串的代碼(不會出現(xiàn)半個漢字)
Java 按照字節(jié)來截取字符串的工具,不會出現(xiàn)半個漢字。一個中文兩個字節(jié),一個英文字符只占 1 個字節(jié)** 1. 通常我們用于前端顯示的時(shí)候,防止標(biāo)題過長2014-01-01RabbitMQ 實(shí)現(xiàn)延遲隊(duì)列的兩種方式詳解
很多場景下我們都需要延遲隊(duì)列。這篇文章主要以RabbitMQ為例來和大家聊一聊延遲隊(duì)列的玩法。文中的代碼具有一定的學(xué)習(xí)價(jià)值,感興趣的同學(xué)可以了解一下2021-12-12java 中用split分割字符串,最后的空格等不被拆分的方法
下面小編就為大家?guī)硪黄猨ava 中用split分割字符串,最后的空格等不被拆分的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02