知識庫 / Spring RSS 訂閱

Spring REST Shell 入門指南

REST,Spring
HongKong
2
03:54 AM · Dec 06 ,2025

1. 概述

在本文中,我們將瞭解 Spring REST Shell 以及它的一些特性。

它是一個 Spring Shell 擴展,因此我們建議您首先閲讀有關它的信息。

2. 引言

Spring REST Shell 是一款命令行 Shell,旨在簡化與 Spring HATEOAS 兼容的 REST 資源交互的工作。

我們不再需要使用諸如 curl 這樣的工具來在 bash 中操作 URL。Spring REST Shell 提供了一種更便捷的方式來與 REST 資源交互。

3. 安裝

如果我們在 macOS 上使用 Homebrew,可以簡單地執行以下命令:

brew install rest-shell

對於其他操作系統用户,需要從 官方 GitHub 項目頁面 下載二進制包,解壓包並找到可執行文件運行:

tar -zxvf rest-shell-1.2.0.RELEASE.tar.gz
cd rest-shell-1.2.0.RELEASE
bin/rest-shell

另一個選項是下載源代碼並執行 Gradle 任務。

git clone git://github.com/spring-projects/rest-shell.git
cd rest-shell
./gradlew installApp
cd build/install/rest-shell-1.2.0.RELEASE
bin/rest-shell

如果一切設置正確,我們將會看到以下問候語:

 ___ ___  __ _____  __  _  _     _ _  __    
| _ \ __/' _/_   _/' _/| || |   / / | \ \   
| v / _|`._`. | | `._`.| >< |  / / /   > >  
|_|_\___|___/ |_| |___/|_||_| |_/_/   /_/   
1.2.1.RELEASE

Welcome to the REST shell. For assistance hit TAB or type "help".
http://localhost:8080:>

4. 入門

我們將使用在另一篇文章中已經開發的 API:https://github.com/eugenp/tutorials/tree/master/spring-web-modules/spring-rest-shelllocalhost:8080 作為基礎 URL。

以下是暴露的端點列表:

  • GET/articles – 獲取所有 Article 對象
  • GET/articles/{id} – 根據 ID 獲取一個 Article 對象
  • GET/articles/search/findByTitle?title={title} – 根據標題獲取一個 Article 對象
  • GET /profile/articles – 獲取 Article 資源的個人資料數據
  • POST/articles – 提供請求體創建新的 Article 對象

Article 類有三個字段:id, title,content。

4.1. 創建新資源

讓我們添加一篇新文章。我們將使用 <strong>post</strong> 命令,通過傳遞一個 JSON <em>String</em>,帶有–data` 參數。

首先,我們需要 <em>遵循</em> 與我們想要添加的資源關聯的 URL。該命令遵循 接受一個相對 URI,將其與 <i>baseUri</i> 連接,並將結果設置為當前位置:

http://localhost:8080:> follow articles
http://localhost:8080/articles:> post --data "{title: "First Article"}"

執行命令後,結果將是:

< 201 CREATED
< Location: http://localhost:8080/articles/1
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Sun, 29 Oct 2017 23:04:43 GMT
< 
{
  "title" : "First Article",
  "content" : null,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/articles/1"
    },
    "article" : {
      "href" : "http://localhost:8080/articles/1"
    }
  }
}

4.2. 發現資源

現在,當我們已經獲取了一些資源後,我們來查找它們。我們將使用 “發現”命令,該命令會顯示當前 URI 下的所有可用資源:

http://localhost:8080/articles:> discover

rel        href                                  
=================================================
self       http://localhost:8080/articles/       
profile    http://localhost:8080/profile/articles
article    http://localhost:8080/articles/1

我們瞭解資源 URI 後,可以使用 get 命令來獲取它:

http://localhost:8080/articles:> get 1

> GET http://localhost:8080/articles/1

< 200 OK
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Sun, 29 Oct 2017 23:25:36 GMT
< 
{
  "title" : "First Article",
  "content" : null,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/articles/1"
    },
    "article" : {
      "href" : "http://localhost:8080/articles/1"
    }
  }
}

4.3. 添加查詢參數

我們可以使用 –params 參數指定查詢參數,以 JSON 片段的形式。

以下是如何通過給定標題獲取文章的示例:

http://localhost:8080/articles:> get search/findByTitle \
> --params "{title: "First Article"}"

> GET http://localhost:8080/articles/search/findByTitle?title=First+Article

< 200 OK
< Content-Type: application/hal+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Sun, 29 Oct 2017 23:39:39 GMT
< 
{
  "title" : "First Article",
  "content" : null,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/articles/1"
    },
    "article" : {
      "href" : "http://localhost:8080/articles/1"
    }
  }
}

4.4. 設置請求頭

命令 headers 允許在會話範圍內管理請求頭。每個請求都將使用這些請求頭髮送。 headers set 命令接受 –name–value 參數,以確定一個請求頭。

我們將添加幾個請求頭併發送一個包含這些請求頭的請求:

http://localhost:8080/articles:>
  headers set --name Accept --value application/json

{
  "Accept" : "application/json"
}

http://localhost:8080/articles:>
  headers set --name Content-Type --value application/json

{
  "Accept" : "application/json",
  "Content-Type" : "application/json"
}

http://localhost:8080/articles:> get 1

> GET http://localhost:8080/articles/1
> Accept: application/json
> Content-Type: application/json

4.5. 將結果寫入文件

並非總是希望將 HTTP 請求的結果打印到屏幕上。有時,我們需要將結果保存到文件中以便進行進一步分析。

<em>–output</em> 參數允許執行此類操作:

http://localhost:8080/articles:> get search/findByTitle \
> --params "{title: "First Article"}" \
> --output first_article.txt

>> first_article.txt

4.6. 從文件讀取 JSON 數據

通常,JSON 數據太大或過於複雜,無法通過使用 <i–data</i> 參數通過控制枱輸入。

此外,我們直接將 JSON 數據輸入命令行時,對 JSON 數據的格式也存在一些限制。

<i–from</i> 參數提供從文件或目錄讀取數據的可能性。

如果值是目錄,則 shell 將讀取所有以 `“.json” 結尾的文件,並使用這些文件的內容執行 POST 或 PUT 操作。

如果參數是一個文件,則 shell 將加載該文件並從該文件中 POST/PUT 數據。

讓我們從文件 <i>second_article.txt</i> 創建下一個文章:

http://localhost:8080/articles:> post --from second_article.txt

1 files uploaded to the server using POST

4.7. 設置上下文變量

我們可以定義當前會話上下文中的變量。var 命令用於指定獲取和設置變量的參數,分別用於獲取和設置變量。

通過與 headers 的類比,參數 -name-value 用於指定新變量的名稱和值:

http://localhost:8080:> var set --name articlesURI --value articles
http://localhost:8080/articles:> var get --name articlesURI

articles

現在,我們將打印出當前上下文中可用的變量列表:

http://localhost:8080:> var list

{
  "articlesURI" : "articles"
}

已確保我們的變量已保存,我們將使用 follow 命令切換到指定的 URI:

http://localhost:8080:> follow #{articlesURI}
http://localhost:8080/articles:> 

4.8. 瀏覽歷史

所有訪問的路徑都會被記錄。<strong >命令 <em >history</em> 顯示這些路徑的按時間順序</strong>:

http://localhost:8080:> history list

1: http://localhost:8080/articles
2: http://localhost:8080

每個URI都與一個數字相關聯,該數字可用於訪問該URI。

http://localhost:8080:> history go 1
http://localhost:8080/articles:>

5. 結論

在本教程中,我們重點介紹了 Spring 生態系統中一個有趣且罕見的工具——命令行工具。

您可以在 GitHub 上找到更多關於該項目的信息:https://github.com/spring-projects/rest-shell#commands

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.