1. 概要

この記事では、SpringRESTShellとその機能のいくつかを見ていきます。

これはSpringShell拡張機能なので、最初にについてを読むことをお勧めします。

2. 序章

Spring RESTシェルは、SpringHATEOAS準拠のRESTリソースの操作を容易にするために設計されたコマンドラインシェルです。

curlなどのツールを使用してbashでURLを操作する必要がなくなりました。SpringREST Shellは、RESTリソースと対話するためのより便利な方法を提供します。

3. インストール

HomebrewでmacOSマシンを使用している場合は、次のコマンドを実行するだけです。

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

もう1つのオプションは、ソースコードをダウンロードして、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を使用します。 localhost: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の3つのフィールドがあります。

4.1. 新しいリソースの作成

新しい記事を追加しましょう。 postコマンドを使用して、–dataパラメーターを使用してJSON文字列を渡します。

まず、追加するリソースに関連付けられているURLをフォローする必要があります。 コマンドfollowは、相対URIを取得し、それを baseUri と連結して、結果を現在の場所として設定します。

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で利用可能なすべてのリソースを明らかにするdiscoverコマンドを使用します。

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リクエストの結果を画面に出力することが常に望ましいとは限りません。 さらに分析するために、結果をファイルに保存する必要がある場合があります。

–output パラメーターを使用すると、次のような操作を実行できます。

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

>> first_article.txt

4.6. ファイルからのJSONの読み取り

多くの場合、JSONデータは大きすぎるか複雑すぎて、 –dataパラメーターを使用してコンソールから入力できません。

また、コマンドラインに直接入力できるJSONデータの形式にはいくつかの制限があります。

–from パラメーターは、ファイルまたはディレクトリーからデータを読み取る可能性を提供します。

値がディレクトリの場合、シェルは“。json” で終わる各ファイルを読み取り、そのファイルのコンテンツでPOSTまたはPUTを実行します。

パラメータがファイルの場合、シェルはファイルとそのファイルからのPOST/PUTデータをロードします。

ファイルsecond_article.txtから次の記事を作成しましょう。

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

1 files uploaded to the server using POST

4.7. コンテキスト変数の設定

現在のセッションコンテキスト内で変数を定義することもできます。 コマンドvarは、変数を取得および設定するためのgetおよびsetパラメーターをそれぞれ定義します。

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. 履歴の表示

私たちが訪れたすべてのパスが記録されます。 コマンド履歴はこれらのパスを時系列で表示します

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をご覧ください。

そして、いつものように、記事で言及されているすべてのコードスニペットは私たちのリポジトリで見つけることができます。