繁体   English   中英

Spring REST 文档 - 避免记录链接

[英]Spring REST Docs - Avoid document the links

我在 SpringBoot 2 应用程序中有这个方法:

@Test
public void shouldEchoTheParameter() throws Exception {
mockMvc.perform(get("/echo").param("echoMessage", "Test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("Test")))
.andDo(document("echo-example",
preprocessResponse(prettyPrint()),
links(linkWithRel("self").ignored().optional()),
requestParameters(
parameterWithName("echoMessage").description("The message to be echoed")),
responseFields(
fieldWithPath("message").
description("The message echoed"))
));
}

我想避免记录这部分有效负载:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/echo"
    }
  }
}

我包括了这个:

links(linkWithRel("self").ignored().optional()),

但我有这个错误:

java.lang.IllegalStateException: No LinkExtractor has been provided and one is not available for the content type application/vnd.pxs.echo.v1+json;charset=UTF-8

links(…)创建一个专门用于记录超媒体链接的片段。 由于您根本不想记录任何链接,因此应避免使用links片段。

相反,修改您对responseFields的使用以忽略_links字段及其下嵌套的所有内容。 REST Docs 将此称为一个小节,可以使用subsectionWithPath(String)进行记录。 你想忽略_links小节,所以你会做这样的事情:

responseFields(subsectionWithPath("_links").ignored(),     
    fieldWithPath("message").description("The message echoed"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM