Spring boot 如何使用SpringRestDocs记录包含JSON对象的请求体

Spring boot 如何使用SpringRestDocs记录包含JSON对象的请求体,spring-boot,spring-restdocs,Spring Boot,Spring Restdocs,我正在使用SpringBoot2来实现RESTAPI服务,并希望用restdocs来记录它 终点 POST /api/tags 请求主体 {"name":"Some Tag", "description":"This is Some Tag"} 用于添加或创建新标记。我已经浏览了restdocs文档,但仍然找不到一种方法来记录请求主体的JSON字段,有人能帮我填充缺少的部分吗 TagRequest request = new TagRequest(); request.setName("So

我正在使用SpringBoot2来实现RESTAPI服务,并希望用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", ...... )));
您需要输入用户请求字段

客户 .post.uri/api/tags .BodyInserters.fromObjectrequest 交换 .expectStatus.isOk .expectBodyInteger.class .消费 文件标签, 请求字段 fieldWithPathname.description。。。。, fieldWithPathname.description。。。。 ;
这在官方文件中有记录:

答案是正确的,特别是在您的情况下,它将如下所示,requestFields fieldWithPathname.descriptionsome_text.typeString.class.getSimpleName,fieldWithPathdescription.descriptionsome_text.typeString.class.getSimpleName