spring-boot - 如何使用 spring restdocs 记录包含 JSON 对象的请求正文
问题描述
我正在使用 spring boot 2 来实现 REST API 服务,并想用 restdocs 记录它。
端点
POST /api/tags
带有请求正文
{"name":"Some Tag", "description":"This is Some Tag"}
用于添加创建新标签。我查看了 restdocs 文档,但仍然找不到记录请求正文的 JSON 字段的方法,谁能帮我填写缺失的部分“......”。
TagRequest request = new TagRequest();
request.setName("Some Tag");
request.setDescription("This is Some Tag");
client.post().uri("/api/tags").body(BodyInserters.fromObject(request)).exchange()
.expectStatus().isOk().expectBody(Integer.class)
.consumeWith(document("add-tag", ...... )));
解决方案
您需要用户requestFields
client
.post().uri("/api/tags")
.body(BodyInserters.fromObject(request))
.exchange()
.expectStatus().isOk()
.expectBody(Integer.class)
.consumeWith(
document("add-tag",
requestFields(
fieldWithPath("name").description("...."),
fieldWithPath("name").description("....")
)
)
);
推荐阅读
- reactjs - 我应该把通用代码放在 Redux 哪里?
- python - 将 Pandas 代码更改为 CUDF 以提高 GPU 利用率
- swift - 如何在 Swift 中创建像 AnyHashable 这样的类型?
- unity3d - AudioSource 附加按钮在另一个场景中不起作用
- c++ - 使用 std::vector 是好是坏(性能方面)
- android - 如何使用 ADB 获取主屏幕中所有应用程序的列表
- visual-studio - 本文档由 Telerik Document Processing 的试用版生成
- django - Django 错误外部 IP:无效的 HTTP_HOST 标头:'www.master.loberon.patrick-thomas.de'
- elasticsearch - Elastic Search 每天更新统计信息
- git - git merge 不会触发带有选项 --no-commit 的 pre-merge-commit 或 post-merge 钩子