Servlet で作成した RESTful API の動作確認方法を書いていきます。
連載記事
- 概要・プロジェクト作成
- Javaコード作成
- 動作確認
アプリの起動
プロジェクトのルートディレクトリの下で、以下のコマンドを実行します。
mvn jetty:run
これで Jetty が起動して、サーブレットの動作確認ができるようになります。
全件取得(GET)
コマンドの実行
コマンドプロンプトで以下のコマンドを実行します。
curl http://localhost:8080/memo
実行結果
以下の JSON を受け取ることができました。
[{"id":1,"txt":"メモ1"},{"id":2,"txt":"メモ2"}]
1件取得(GET)
コマンドの実行
curl http://localhost:8080/memo/1
実行結果
{"id":1,"txt":"メモ"}
登録(POST)
コマンドの実行
curl http://localhost:8080/memo -X POST -d "txt=Memo"
実行結果
{"id":3,"txt":"Memo"}
更新(PUT)
コマンドの実行
curl http://localhost:8080/memo/1 -X PUT -d "txt=Memo"
実行結果
{"id":1,"txt":"Memo"}
削除(DELETE)
コマンドの実行
curl http://localhost:8080/memo/1 -X DELETE -v
実行結果
* Trying 127.0.0.1:8080... * Connected to localhost (127.0.0.1) port 8080 (#0) > DELETE /memo/1 HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.83.1 > Accept: */* > * Mark bundle as not supporting multiuse < HTTP/1.1 204 No Content < Date: Tue, 22 Nov 2022 16:51:29 GMT < Server: Jetty(11.0.12) < * Connection #0 to host localhost left intact
レスポンスのボディはありませんが、HTTP 204 を確認することができます。
アプリの終了
Jetty を起動したウィンドウで Ctrl+C
を押すとアプリが停止します。