【问题标题】:Document each element of an array using spring-restdocs使用 spring-restdocs 记录数组的每个元素
【发布时间】:2018-04-29 20:14:56
【问题描述】:

我的响应是一个 json 数组。每个元素都有它的含义,我可以描述它。

我的数组:

["1525032694","300","true"]

我在文档中找到了一个示例,该示例描述了一个数组和每个相同的元素:

https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-request-response-payloads-fields-reusing-field-descriptors

但我想知道如何将其描述为:

current timestampseconds to next measurementshould perform fota

我目前的测试:

webTestClient.post().uri("/api//measurement/${SampleData.deviceId}")
    .syncBody(SampleData.sampleMeasurement())
    .exchange()
    .expectStatus()
    .isOk
        .expectBody()
        .consumeWith(document("add-measurement", responseFields(
                fieldWithPath("[]")
                        .description("An array of device settings"))
        ))

【问题讨论】:

  • 我没有使用过 spring-restdocs。 fieldWithPath("0").description("current timestamp") 有效吗?在 JavaScript 中,每个数组元素都可以像使用 "0""1""2"、...、"${n}"... 的任何其他属性一样访问,但这不是 JavaScript。 :-) 如果"0" 不起作用,我想知道"[0]" 是否起作用......
  • 您可以控制 API 响应吗?为什么不返回正确的对象而不是设备设置的数组?
  • 不幸的是我无法控制这一点,这就是为什么我需要更准确地记录响应

标签: arrays json kotlin spring-restdocs


【解决方案1】:

好的,这很容易:

        responseFields(
            fieldWithPath("[0]").description("Current timestamp"),
            fieldWithPath("[1]").description("Device mode"),
            fieldWithPath("[2]").description("Device parameter")
        ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多