gpt4 book ai didi

java - 如何在 Spring REST 文档中记录树

转载 作者:行者123 更新时间:2023-11-30 01:51:39 25 4
gpt4 key购买 nike

我有一棵返回以下结构的树:

[{
"data":
{
"id": 15,
"permissionId": "perm1",
"name": "Events"
},
"children": [
{
"data":
{
"id": 16,
"permissionId": "perm2",
"name": "Report",
"parentRightDictionaryItemId": 15
},
"children": [
{
"data":
{
"id": 17,
"permissionId": "perm3",
"name": "Construct",
"parentRightDictionaryItemId": 16
}
}],

}]

}]

而且我不明白如何记录这棵树的字段,因为它可能非常深。我想做的,这个函数返回记录字段的结构:

protected List<FieldDescriptor> getResponseFieldDescriptor(String prefix) {
List<FieldDescriptor> fields = new ArrayList<>();

fields.add(fieldWithPath(prefix + "data").description("data").type(OBJECT));
fields.add(fieldWithPath(prefix + "data.id").description("id").type(NUMBER));
fields.add(fieldWithPath(prefix + "data.permissionId").description("permissionId").type(STRING));
fields.add(fieldWithPath(prefix + "data.name").description("name").type(STRING));
fields.add(fieldWithPath(prefix + "children").description("children").type(ARRAY).optional()); // I want this to be enough, but that's not enough

return fields;
}

如果子项数量为空,我的函数可以正常工作。但是在有 child 的情况下,会返回一个错误,表明我没有记录整个树结构。这是很多。怎么办?

最佳答案

如果您只想记录部分响应并且不希望测试在未记录的部分上失败,我认为至少有两种方法。可以使用 subsectionWithPath 记录响应的一部分:

If you don’t want to provide detailed documentation for all of the fields, an entire subsection of a payload can be documented.

另一种方法是使用relaxedResponseFields:

Fields can also be documented in a relaxed mode where any undocumented fields will not cause a test failure. To do so, use the relaxedRequestFields and relaxedResponseFields methods on org.springframework.restdocs.payload.PayloadDocumentation.

参见https://docs.spring.io/spring-restdocs/docs/2.0.3.RELEASE/reference/html5/#documenting-your-api-request-response-payloads-fields了解更多详情。

关于java - 如何在 Spring REST 文档中记录树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809428/

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