rest - Spring REST Docs - 未记录的部分:
问题描述
我有一个使用 Spring REST Docs 的 springBoot 2.1.9.RELEASE 应用程序。
我有这个有效载荷
{
"hostel" : [ {
"bookingHostelIdentifier" : {
"internalMasterIdentifier" : {
"id" : "987654321"
}
}
}, {
"bookingHostelIdentifier" : {
"customerIdentifier" : {
"id" : "23456789"
}
}
}, {
"bookingHostelIdentifier" : {
"internalMasterIdentifier" : {
"id" : "87654321"
}
}
} ]
}
我这样记录的
fieldWithPath("hostel[]").description("The list of hostels"),
fieldWithPath("hostel[].name").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").description("The list of hostels")
但我得到了这个例外
org.springframework.restdocs.snippet.SnippetException:
Fields with the following paths were not found in the payload:
[hostel[].bookingHostelIdentifier.internalMasterIdentifier.id, hostel[].bookingHostelIdentifier.customerIdentifier.id]
解决方案
由于internalMasterIdentifier
和customerIdentifier
字段并不总是出现在 a 中bookingHostelIdentifier
,因此您应该将这些字段或id
嵌套在它们下方的字段标记为可选。
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").optional().description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").optional().description("The list of hostels")
推荐阅读
- angular - “HttpSentEvent”类型上不存在“已加载”属性
- node.js - 第二次更新 mongo 文档中的时间戳导致 ConflictingUpdateOperators
- javascript - 获取循环选择的输入值,并在其变化时求和里面的值
- android - Android 上的电容器失败并出现错误:“ERR_SDK_PACKAGE_NOT_FOUND:SDK 包未按位置找到:D:\Android_SDK_HOME\platform-tools。”
- c# - 如何防止ListView排序阻塞程序
- php - 如何在php codenighter中的特定索引处添加数组值
- javascript - 如何在 VSCode 中运行 SCrypt
- java - 填充html以生成pdf openhtmltopdf?
- node.js - 使用node.js rest api在客户端发送数据可以每5s完成一次吗?
- javascript - 在表格或图表中动态过滤最近 3 个月