简体   繁体   English

Spring REST 文档 - 避免记录链接

[英]Spring REST Docs - Avoid document the links

I have this method in a SpringBoot 2 application:我在 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"))
));
}

I want to avoid to document this part of the payload:我想避免记录这部分有效负载:

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

I included this:我包括了这个:

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

but i have this error:但我有这个错误:

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(…) creates a snippet that is specifically for documenting hypermedia links. links(…)创建一个专门用于记录超媒体链接的片段。 As you don't want to document any links at all, you should avoid using the links snippet.由于您根本不想记录任何链接,因此应避免使用links片段。

Instead, modify your use of responseFields to ignore the _links field and everything nested beneath it.相反,修改您对responseFields的使用以忽略_links字段及其下嵌套的所有内容。 REST Docs refers to this as a subsection and it can be documented using subsectionWithPath(String) . REST Docs 将此称为一个小节,可以使用subsectionWithPath(String)进行记录。 You want to ignore the _links subsection so you'd do something like this:你想忽略_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