1. 概述
本文将介绍 Spring REST Shell 及其核心特性。作为 Spring Shell 的扩展组件,建议先阅读 相关文档 以获取基础背景知识。
2. 简介
Spring REST Shell 是一个专为操作符合 Spring HATEOAS 规范的 REST 资源而设计的命令行工具。 它彻底解放了开发者:不再需要像使用 curl 那样在 bash 中手动拼接 URL。该工具提供了更直观的 REST 资源交互方式。
3. 安装
3.1 macOS 安装(使用 Homebrew)
brew install rest-shell
3.2 其他操作系统安装
- 下载二进制包(官方 GitHub 项目页)
- 解压并执行:
tar -zxvf rest-shell-1.2.0.RELEASE.tar.gz cd rest-shell-1.2.0.RELEASE bin/rest-shell
3.3 源码编译安装
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
3.4 启动成功标志
___ ___ __ _____ __ _ _ _ _ __
| _ \ __/' _/_ _/' _/| || | / / | \ \
| v / _|`._`. | | `._`.| >< | / / / > >
|_|_\___|___/ |_| |___/|_||_| |_/_/ /_/
1.2.1.RELEASE
Welcome to the REST shell. For assistance hit TAB or type "help".
http://localhost:8080:>
4. 快速上手
我们使用 现有 API 进行演示,基础 URL 为 localhost:8080
。暴露的接口包括:
-
GET /articles
– 获取所有文章 -
GET /articles/{id}
– 根据 ID 获取文章 -
GET /articles/search/findByTitle?title={title}
– 根据标题获取文章 -
GET /profile/articles
– 获取文章资源的元数据 -
POST /articles
– 创建新文章
文章对象包含三个字段:id
、title
和 content
。
4.1 创建资源
使用 post
命令配合 --data
参数提交 JSON 数据:
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 资源发现
使用 discover
命令列出当前 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
通过 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
命令管理会话级别的请求头:
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 结果保存到文件
使用 --output
参数将响应写入文件:
http://localhost:8080/articles:> get search/findByTitle \
> --params "{title: "First Article"}" \
> --output first_article.txt
>> first_article.txt
4.6 从文件读取 JSON
--from
参数支持从文件或目录读取数据:
- 目录:自动读取所有
.json
文件 - 文件:读取指定文件内容
http://localhost:8080/articles:> post --from second_article.txt
1 files uploaded to the server using POST
4.7 上下文变量管理
var
命令管理会话变量:
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"
}
使用变量切换 URI:
http://localhost:8080:> follow #{articlesURI}
http://localhost:8080/articles:>
4.8 历史记录管理
history
命令记录访问路径:
http://localhost:8080:> history list
1: http://localhost:8080/articles
2: http://localhost:8080
通过编号快速跳转:
http://localhost:8080:> history go 1
http://localhost:8080/articles:>
5. 总结
本文介绍了 Spring 生态中一个独特但实用的命令行工具。更多细节可参考 GitHub 项目,文中的所有代码示例可在 代码仓库 中找到。