0

我正在使用Spring REST Docs library编写我的 rest 服务的文档。

我遇到的一个问题是我接受一个带有 JSON 结构的 POST 请求作为没有任何名称的输入。

请求看起来像这样:

POST /images?limit=3&offset=12 HTTP/1.1
Content-Type: application/json
Host: localhost:8080
Content-Length: 289

{"acquisitionWindow":0,"platformName":"myPlatform","requireImageFile":false,"requireImageFileOrQuicklook":false,"strictPolygon":false,"showInternal":false,"imageNames":["%"],"startTimeOfYear":0,"stopTimeOfYear":0,"resolutionLowerBound":0.0,"resolutionUpperBound":0.0,"reducedResolution":0}

我想记录输入结构,但到目前为止还没有办法做到这一点。

该文档描述了这样的内容:

this.mockMvc.perform(get("/users?page=2&per_page=100")) 
    .andExpect(status().isOk())
    .andDo(document("users", requestParameters( 
            parameterWithName("page").description("The page to retrieve"), 
            parameterWithName("per_page").description("Entries per page") 
    )));

但是我的参数没有名字。

我已经尝试过:

requestParameters(
    // TODO: Insert ImageSearch here
    parameterWithName("{}").description("ImageSearch Structure "))

我也尝试使用空名称("")和数组表示法("[]")。到目前为止没有运气。

是否可以记录未命名的参数,还是应该将其包装在另一个结构中?

谢谢,

4

1 回答 1

1

您可以使用requestFieldsonorg.springframework.restdocs.payload.PayloadDocumentation记录请求中发送的 JSON 有效负载。例如:

this.mockMvc.perform(get("/users?page=2&per_page=100")) 
    .andExpect(status().isOk())
    .andDo(document("users", requestFields( 
            fieldWithPath("acquisitionWindow").description("…"), 
            fieldWithPath("platformName").description("…"))));

有关更多详细信息,请参阅文档的请求和响应有效负载部分

于 2016-10-05T14:39:53.903 回答