Spring REST文档-未记录的零件:

时间:2019-10-10 12:06:46

标签: rest spring-boot spring-mvc spring-rest spring-restdocs

我有一个使用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]

1 个答案:

答案 0 :(得分:0)

由于internalMasterIdentifiercustomerIdentifier字段并不总是出现在bookingHostelIdentifier中,因此您应该将这些字段或嵌套在它们下面的id字段标记为可选

fieldWithPath("hostel[].bookingHostelIdentifier.internalMasterIdentifier.id").optional().description("The list of hostels"),
fieldWithPath("hostel[].bookingHostelIdentifier.customerIdentifier.id").optional().description("The list of hostels")