首页 > 解决方案 > 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]

标签: restspring-bootspring-mvcspring-restspring-restdocs

解决方案


由于internalMasterIdentifiercustomerIdentifier字段并不总是出现在 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")

推荐阅读