天天看點

SpringBoot-靜态資源通路&REST風格請求

作者:黑芝麻湯圓他爹

1.SpringBoot靜态資源通路

1.1基本介紹

  1. 隻要靜态資源是放在類路徑下的:/static、/public、/resources、/META-INF/resources,則可以直接被通路。根據是:
  2. SpringBoot在啟動的時候會去解析WebProperties.java檔案:
  3. private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};//注意這裡不能直接通路classpath:類路徑,也就是說不能直接通路項目的resources目錄
  4. 類路徑:如maven的類路徑就是:src/main/resources目錄,因為在該目錄的資源檔案運作後會直接放在target/classes目錄下
  5. 常見的靜态資源:JS,CSS,圖檔(.jpg .png .gif .bmp .svg),字型檔案(Fonts)等
  6. 通路方式,預設情況下:項目根路徑/+靜态資源名,如
  7. http://localhost:8080/hi.html
  8. 可以通過配置檔案去修改WebMvcProperties.java中的路徑:

1.2快速入門

  1. 建立SpringBoot相關環境,在pom.xml中導入如下依賴
  2. <!--導入SpringBoot父工程--><parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.5.3</version></parent> <dependencies> <!--導入場景啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
  3. 建立相關靜态資源目錄,并放入測試圖檔
  4. 編寫主程式并啟動
  5. package com.li; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author 李 * @version 1.0 */@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}
  6. 在浏覽器分别通路http://localhost:8080/2.png、http://localhost:8080/4.png、http://localhost:8080/5.png、http://localhost:8080/6.png,都可以通路到圖檔。
  7. 注意在預設情況下,不能直接通路到項目resources目錄的資源

1.3注意事項和細節

  1. 靜态資源通路原理:靜态映射為/**,也就是對所有請求進行攔截,服務端接收到請求後,先看Controller能不能處理(即先去找有沒有對應映射路徑的Controller),不能處理的請求交給靜态資源處理器,如果靜态資源找不到,則響應404頁面
  2. 路徑沖突問題:因為SpringBoot先走Controller的映射,然後走靜态資源處理器。兩者可能存在路徑沖突。這時我們就需要修改預設的靜态資源通路路徑,解決路徑沖突。
  3. 解決路徑沖突,改變靜态資源通路字首:在配置檔案中設定static-path-pattern屬性,如下:
  4. spring: mvc: static-path-pattern: /myres/** #修改靜态資源通路的字首
  5. 這樣靜态資源通路的字首就變為/myres/,http://localhost:8080/2.png===>http://localhost:8080/myres/pen.png
  6. 修改預設的靜态資源路徑:比如我們希望在類路徑下增加img目錄,并能通路到資源。解決方法同樣是在配置檔案中修改屬性,如下:
  7. 在浏覽器通路http://localhost:8080/myres/pen.png即可通路到pen.png圖檔
  8. 1.如果你配置了static-location屬性,那麼以你指定的路徑為準,原來的預設路徑不再生效
  9. 2.請保證運作時資源已經拷貝到了target目錄下

2.REST風格請求處理

2.1基本介紹

Rest風格支援,就是使用HTTP請求方式動詞表示對資源的操作

  • GET-擷取資源(查)
  • DELETE-删除資源(删)
  • PUT-修改資源(改)
  • POST-儲存資源(增)

2.2應用執行個體

示範SpringBoot中如何實作Rest風格的增删改查

(1)建立Controller

package com.li.controller; import org.springframework.web.bind.annotation.*; /** * @author 李 * @version 1.0 */@RestController//Controller+ResponseBodypublic class MonsterController {    //因為請求方式不同,是以url不會沖突     @GetMapping("/monster")    public String getMonster() {        return "GET-查詢妖怪";    }     @PostMapping("/monster")    public String saveMonster() {        return "POST-儲存妖怪";    }     @PutMapping("/monster")    public String putMonster() {        return "PUT-修改妖怪";    }     @DeleteMapping("/monster")    public String delMonster() {        return "DELETE-删除妖怪";    }}           

(2)使用postman,分别使用不同的方式請求

SpringBoot-靜态資源通路&amp;REST風格請求
SpringBoot-靜态資源通路&amp;REST風格請求

需要注意,postman是直接發出不同的請求,如果是表單請求,需要建立過濾器将post轉化為不同的請求

(3)使用頁面表單進行rest風格的請求

  • 使用表單進行rest風格請求需要配置檔案啟用filter功能(見2.3)
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Rest</title></head><body><h1>Rest風格的請求</h1><form action="/monster" method="post">    <!--通過隐藏域發送_method指定值-->    <input type="hidden" name="_method" value="put"><!--其他請求同理-->    u:<input type="text" name="name"><br/>    <input type="submit" value="送出表單"></form></body></html>           
SpringBoot-靜态資源通路&amp;REST風格請求
SpringBoot-靜态資源通路&amp;REST風格請求

2.3注意事項和細節

  1. 如果用戶端是postman,它可以直接發送PUT、DELETE等請求,可以不設定Filter
  2. 如果要SpringBoot支援頁面表單的Rest功能,則需要注意:
  3. Rest風格請求的核心Filter:HiddenHttpMethodFilter,表單請求會被該Filter攔截,擷取到該表單的_method值,再判斷是否為PUT/DELETE/PATCH(PATCH是新引入的,用來對已知資源進行局部更性)
  4. 如果要SpringBoot支援頁面表單的Rest功能,需要在配置檔案中啟用filter功能,否則Rest風格在頁面表單上無效。配置如下:
  5. spring: mvc: hiddenmethod: filter: enabled: true #開啟頁面表單的Rest風格請求功能
  6. 注意@RestController是@Controller+@ResponseBody注解的組合,如果隻有@Controller,那麼方法傳回的字元串會:
  7. (1)沒有配置視圖解析器時,和其他方法的映射路徑比對
  8. (2)配置了視圖解析器,視圖解析器解析轉發到比對名稱的資源,配置方法如下:
  9. spring: mvc: view: #視圖解析器 suffix: .html prefix: / #注意這裡需要和目前的static-path-pattern屬性比對