gpt4 book ai didi

java - restdocs SnippetException 由于来自 spring-data-rest 的 HAL "_links"元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:18 30 4
gpt4 key购买 nike

我的应用程序正在使用 spring-data-restspring-restdocs。我的设置非常标准;几乎完全从文档中复制,但我在下面包含了示例以防遗漏某些内容。当我的 mvc 测试运行时,它失败了:

org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"_links" : {
"self" : {
"href" : "https://my-api/item/10"
},
"item" : {
"href" : "https://my-api/item/10"
}
}
}

这是我的测试代码:

@Rule
public JUnitRestDocumentation restDocs = new JUnitRestDocumentation("target/generated-snippets");
// ...
mockMvc = webAppContextSetup(wac) //WebApplicationContext
.apply(documentationConfiguration(restDocs)
.uris()
.withHost("my-api")
.withPort(443)
.withScheme("https"))
.build();
// ....
mockMvc.perform(get("/items/{id}", "10"))
.andDo(documentation)

这是堆栈:

at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:176)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:100)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.example.my.api.domain.MyRepositoryRestTest.findOne(MyRepositoryRestTest.java:36)

如何让 spring-restdocsspring-data-rest 发挥良好的作用?


编辑(S):

我的文档实例定义如下:

ResultHandler documentation = document("items/findOne",
preprocessRequest(prettyPrint(), maskLinks()),
preprocessResponse(prettyPrint()),
responseFields(
fieldWithPath("name").description("Item name.")
// Bunch more
));

正如@meistermeier 指出的那样,(在 the restdocs docs for ignoring links 之后,我可以添加

links(linkWithRel("self").ignored(),
linkWithRel("_self").ignored().optional()) // docs suggest this. /shrug

但这仍然给我留下了:

SnippetException: Links with the following relations were not documented: [item]

Seems like the _links are always going to have that self-reference back to the same entity, right?How do I cleanly handle this without ignoring an entity-specific link for every test, like:

links(linkWithRel("item").ignored())

即使我添加上面的行(这样所有字段self _self curiesitem 都是ignored() 和/或optional()), 测试的结果返回到本题顶部的原始错误。

最佳答案

Seems like the _links are always going to have that self-reference back to the same entity, right?

是的,没错。

对于忽略 a small github sample 中的某些链接,我可能有您的解决方案.特别是部分:

mockMvc.perform(RestDocumentationRequestBuilders.get(beerLocation)).andExpect(status().isOk())
.andDo(document("beer-get", links(
linkWithRel("self").ignored(),
linkWithRel("beerapi:beer").description("The <<beers, Beer resource>> itself"),
linkWithRel("curies").ignored()
),
responseFields(
fieldWithPath("name").description("The name of the tasty fresh liquid"),
fieldWithPath("_links").description("<<beer-links,Links>> to other resources")
)
));

我完全忽略所有“生成的” 字段,只为该域创建一个文档条目。你的item链接将是我的beerapi:beer .

我真的不知道这里的最佳实践是什么,但我总是会尽可能多地记录,因为您可以尽可能使用 asciidoctor 链接(如 <<beer-links,Links>> )来引用其他部分和更多文档。

关于java - restdocs SnippetException 由于来自 spring-data-rest 的 HAL "_links"元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107555/

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