Spring rest docs嵌套对象

时间:2016-09-21 14:57:02

标签: spring-restdocs

我们需要以下列形式提供对来电的回复:

{
  "3" : {
    "id" : "3",
    "parent" : "1",
    "title" : "Folder 2",
    "folder" : true
  },
  "4" : {
    "id" : "4",
    "parent" : "2",
    "title" : "Folder 1.1",
    "folder" : true
  },
  "doc:3" : {
    "parent" : "3",
    "title" : "Document 3",
    "folder" : false
  },
  "doc:2" : {
    "parent" : "2",
    "title" : "Document 2",
    "folder" : false
  },
  "doc:1" : {
    "parent" : "3",
    "title" : "Document 1",
    "folder" : false
  }
}

我该如何记录?问题是该对象是一个以EntryId为键的结构,并且在其文档时不包含id。有没有可以使用变量来定义键的选项?或者推荐在responseFields中使用什么?

1 个答案:

答案 0 :(得分:1)

我遇到了Andy Wilkinson在评论中指出的相同问题,而不是在ascii.doc中硬编码,我是这样做的:

  ,relaxedResponseFields(
     fieldWithPath("key").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY)).type(MessageProperties.TYPE_STRING).optional(),
     fieldWithPath("value").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE)).type(MessageProperties.TYPE_MAP).optional(),
     fieldWithPath("value[].key").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE_KEY)).type(MessageProperties.TYPE_LONG).optional(),
     fieldWithPath("value[].value").description(getMessage(MessageProperties.TRANSLATION_RESOURCE_MAP_KEY_VALUE_KEY_VALUE)).type(MessageProperties.TYPE_STRING).optional()
  )

检查How to document response fields for an object as Map(HashMap)以了解问题的更多信息,以及它是否真的与您的相关。

相关问题