【发布时间】:2017-06-24 05:01:45
【问题描述】:
如何使用 Spring REST Docs 记录子文档中的链接?
给定以下 JSON 文档:
{
"links": {
"alpha": "http://example.com/alpha",
"beta": "http://example.com/beta"
}
}
我可以按照reference docs 中的建议通过实现自定义LinkExtractor 来记录links(我的工作实现与HalLinkExtractor 非常相似):
mockMvc.perform(get("/"))
.andDo(document("root-resource",
links(customLinkExtractor(),
linkWithRel("alpha").description("Link to the Alpha resource"),
linkWithRel("beta").description("Link to the Beta resource")
)
));
但是,我的 JSON 文档在其他地方包含 links 子文档,例如
{
"links": {
"alpha": "http://example.com/alpha",
"beta": "http://example.com/beta",
},
"foo": {
"links": {
"gamma": "https://gamma.com/",
"delta": "https://delta.com/"
}
}
}
如何记录与foo 子文档关联的links 文档?理想情况下,我想做这样的事情:
mockMvc.perform(get("/"))
.andDo(document("root-resource",
links(customLinkExtractor(),
linkWithRel("alpha").description("Link to the Alpha resource"),
linkWithRel("beta").description("Link to the Beta resource")
),
links(jsonPath("$.foo"),
customLinkExtractor(),
linkWithRel("gamma").description("Link to the Gamma resource"),
linkWithRel("delta").description("Link to the Delta resource")
)
));
当然,这不起作用,因为没有jsonPath(..) 方法。还有哪些其他选择?
我猜如果您使用HalLinkExtractor 并尝试在_embedded 子文档中记录链接,也会出现同样的问题(参见draft-kelly-json-hal 中的示例)。
【问题讨论】:
标签: spring-restdocs