Spring RESTシェルの紹介

Spring REST Shellの概要

1. 概要

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

これはSpringShell拡張機能であるため、最初にabout itを読み取ることをお勧めします。

2. 前書き

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

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

3. インストール

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

brew install rest-shell

他のオペレーティングシステムのユーザーの場合、the official GitHub project pageからバイナリパッケージをダウンロードし、パッケージを解凍して、実行する実行可能ファイルを見つける必要があります。

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. 入門

APIalready developed for another articleを使用します。 localhost:8080はベースURLとして使用されます。

公開されているエンドポイントのリストは次のとおりです。

  • GET/articles –すべてのArticlesを取得

  • 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. 新しいリソースの作成

新しい記事を追加しましょう。 the post command passing a JSON String with the –data parameterを使用します。

まず、追加するリソースに関連付けられているURLをfollowする必要があります。 コマンド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. リソースの発見

さて、いくつかのリソースができたら、それらを見つけましょう。 the discover command which reveals all available resources at the current 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. ヘッダーの設定

The command called headers allows managing headers within the session scope –すべてのリクエストはこれらのヘッダーを使用して送信されます。 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リクエストの結果を画面に出力することが常に望ましいとは限りません。 Sometimes, we need to save the results in a file for further analysis.

–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. コンテキスト変数の設定

現在のセッションコンテキスト内で変数を定義することもできます。 The command var defines the get and set parameters for getting and setting a variable respectively.

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

訪問したすべてのパスが記録されます。 The command history shows these paths in the chronological order

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エコシステムの興味深い珍しいツールであるコマンドラインツールに焦点を当てました。

プロジェクトover on GitHubに関する詳細情報を見つけることができます。

そして、いつものように、記事で言及されているすべてのコードスニペットはour repositoryにあります。