Spring REST Doc unamed request参数

时间:2016-10-05 13:20:57

标签: java spring spring-restdocs

我正在使用Spring REST Docs library撰写我的休息服务的文档。

我遇到的一个问题是我接受一个带有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 "))

我还尝试使用空名(“”),以及数组符号(“[]”)。到目前为止没有运气。

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

谢谢,

1 个答案:

答案 0 :(得分:1)

您可以使用requestFields上的org.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("…"))));

有关更多详细信息,请参阅Request and response payloads section of the documentation