java - 获取 SnippetException:未记录有效负载的以下部分:数组元素错误
问题描述
我试图在这里使用 Spring-RestDocs 记录我的 REST 服务。但到目前为止,我还无法记录数组元素。
测试方法:
@Test
public void listAll() throws Exception {
MockHttpServletRequestBuilder requestBuilder =
RestDocumentationRequestBuilders.post("/diagnosis/search/{term}", "headache")
.header("Authorization",TestHelper.TOKEN).with(csrf());
TestHelper.httpRequestBuilder(requestBuilder, new SearchEntity("5b55aabd0550de0021097b64",Arrays.asList("PL01", "PL02"),true));
MvcResult result = mockMvc.perform(requestBuilder)
.andDo(DiagnosisDocument.documentSearchTerm())
.andExpect(status().isOk())
.andReturn();
MockHttpServletResponse response = result.getResponse();
System.out.println(response.getContentAsString());
assertEquals(HttpStatus.OK.value(), response.getStatus());
}
记录方法:
public static ResultHandler documentSearchTerm() {
return document("search-diagnosis", pathParameters(
parameterWithName("term").description("Term")),
requestFields(fieldWithPath("clinicId").description("bla bla")),
requestFields(fieldWithPath("isGlobalSearch").description("bla bla")),
requestFields(subsectionWithPath("[].planIds").description("bla bla")),
responseAPI(true));
}
SearchEntity 类:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class DiagnosisSearchEntiry {
private String clinicId;
private List<String> planIds = new ArrayList<>();
private boolean isGlobalSearch;
}
But in this implementation, im getting following exception and the test fails.
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"planIds" : [ "PL01", "PL02" ],
"globalSearch" : true
}
我收到这个错误有什么特别的原因吗?我记录错了吗?提前致谢
解决方案
当DiagnosisSearchEntiry
被序列化为 JSON 时,该isGlobalSearch
字段被映射到 JSON 中名为globalSearch
. 您的请求字段路径需要更新以反映这一点:
requestFields(fieldWithPath("globalSearch").description("bla bla"))
该路径[].planIds
正在寻找具有planIds
字段的对象数组。它会像这样匹配 JSON:
[
{
"planIds": ["PL01", "PL02"]
},
{
"planIds": ["PL03", "PL04"]
}
]
您正在记录的 JSON 的结构如下:
{
"clinicId": "the clinic id",
"planIds": [ "PL01", "PL02" ],
"globalSearch": true
}
要记录计划 ID 数组,路径应为planIds.[]
:
requestFields(subsectionWithPath("planIds.[]").description("bla bla"))
推荐阅读
- google-apps-script - 当脚本在 Google Apps 脚本中运行时,有没有办法动态添加一个工作表?
- angular - 如何修复第一次登录时不显示侧边菜单按钮
- java - Oracle/JDBC:行的正确时区问题
- django - 如果项目在 python 列表中,则注释布尔字段
- ruby-on-rails - 我可以在我的应用程序菜单中查询 DEVISE current_user 助手吗?
- java - 动作事件如何与玩家的回合一起工作,需要帮助显示用户在计算机显示它玩过什么牌之前玩过哪张牌
- html - 无法让我的标签在 GitHub 网页中工作(但在 Codepen 上工作正常)
- php - 对相关模型进行分组和计数
- twilio - 如何正确循环通过 Twilio 功能发送短信
- typescript - Typescript 函数可以返回 Promise
作为承诺 ?