Java 获取代码段异常:未记录负载的以下部分:数组元素错误
我试图在这里使用SpringRestDocs记录我的REST服务。但到目前为止,我还无法记录数组元素 试验方法:Java 获取代码段异常:未记录负载的以下部分:数组元素错误,java,json,spring,spring-restdocs,Java,Json,Spring,Spring Restdocs,我试图在这里使用SpringRestDocs记录我的REST服务。但到目前为止,我还无法记录数组元素 试验方法: @Test public void listAll() throws Exception { MockHttpServletRequestBuilder requestBuilder = RestDocumentationRequestBuilders.post("/diagnosis/search/{term}", "headache")
@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
}
我犯这个错误有什么特别的原因吗?我把它记录错了吗?提前感谢当DiagnosisSearchEntity序列化为JSON时,isGlobalSearch字段映射到JSON中名为globalSearch的字段。您的请求字段路径需要更新以反映以下情况:
requestFields(fieldWithPath("globalSearch").description("bla bla"))
路径[]。planIds正在查找包含planIds字段的对象数组。它将与JSON匹配,如下所示:
[
{
"planIds": ["PL01", "PL02"]
},
{
"planIds": ["PL03", "PL04"]
}
]
{
"clinicId": "the clinic id",
"planIds": [ "PL01", "PL02" ],
"globalSearch": true
}
正在记录的JSON的结构如下所示:
[
{
"planIds": ["PL01", "PL02"]
},
{
"planIds": ["PL03", "PL04"]
}
]
{
"clinicId": "the clinic id",
"planIds": [ "PL01", "PL02" ],
"globalSearch": true
}
要记录计划ID数组,路径应为planIds。[]: