gpt4 book ai didi

java - 如何使用 Spring Rest Docs 记录对象列表

转载 作者:行者123 更新时间:2023-12-01 19:54:10 25 4
gpt4 key购买 nike

我正在使用 Spring Rest 文档来记录我的 REST API。我在集成测试中使用mockMVC,并且想要记录以下 JSON 响应:

GET/api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7

{
"id": "3b658b39-4264-4995-99d8-90a1672a75a7",
"name": "Foo",
"nickname": "Bar",
"phones": [
{
"id": "6ca3a963-bacb-4770-a470-5902b4a17b77",
"alias": "Personal Phone 1",
"countryCode": "55",
"areaCode": "34",
"number": "99999-9999"
},
{
"id": "f3a3726b-b5f8-4652-a044-7bf3d95a37de",
"alias": "Personal Phone 2",
"countryCode": "55",
"areaCode": "34",
"number": "88888-8888"
}
]
}

我如何记录上面的手机列表?您可以使用以下代码片段,该代码片段使用 Spring REST 文档来记录此 API 操作:

this.mockMvc.perform(
get("/api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7")
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("customer").withResponseFields(
fieldWithPath("id").description("Unique identifier"),
fieldWithPath("name").description("Customer full name"),
fieldWithPath("nickname").description("How the customer wants to be called")));

最佳答案

根据此链接https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-request-response-payloads-fields-json

当记录通过数组对象嵌套时,可以这样使用:

this.mockMvc.perform(
get("/api/v1/customers/3b658b39-4264-4995-99d8-90a1672a75a7")
.accept(APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("customer").withResponseFields(
fieldWithPath("id").description("Unique identifier"),
fieldWithPath("name").description("Customer full name"),
fieldWithPath("nickname").description("How the customer wants to be called"),
fieldWithPath("phones[].id").description("PHONE ID DESCRIPTION"),
fieldWithPath("phones[].alias").description("PHONE ALIAS DESCRIPTION"),
fieldWithPath("phones[].countryCode").description("PHONE COUNTRY CODE DESCRIPTION"),
fieldWithPath("phones[].areaCode").description("PHONE AREA CODE DESCRIPTION")
fieldWithPath("phones[].number").description("PHONE NUMBER DESCRIPTION")
));

关于java - 如何使用 Spring Rest Docs 记录对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50156492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com