java - Spring REST Doc未命名请求参数

标签 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 "))

我还尝试过使用空名称("")和数组符号("[]")。到目前为止还没有运气。

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

谢谢

最佳答案

您可以在 org.springframework.restdocs.payload.PayloadDocumentation 上使用 requestFields 来记录请求中发送的 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了解更多详细信息。

关于java - Spring REST Doc未命名请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875144/

相关文章:

java - UInt8 数组到 Base64 字符串 Swift(包括 Java 示例)

java - Spring MVC 路径模式匹配不处理破折号

java - CGLib 代理和非空构造函数

maven - 传递依赖项拉取低于 POM 中声明的版本

java - Spring REST 文档抛出 MustacheException

java - JPA 中实体列表的搜索和排序问题

java - 有什么方法可以防止 Mock 实现出现 "resource"警告吗?

java - 停止查看空堆栈

java - 创建多个 InternalResourceViewResolver

junit - Spring REST 文档 : How to migrate Rule to JUnit 5