我有一个使用Spring REST文档的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]
答案 0 :(得分:0)
由于internalMasterIdentifier
和customerIdentifier
字段并不总是出现在bookingHostelIdentifier
中,因此您应该将这些字段或嵌套在它们下面的id
字段标记为可选
fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").optional().description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").optional().description("The list of hostels")