简体   繁体   中英

Spring REST Docs - Avoid document the links

I have this method in a SpringBoot 2 application:

@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. As you don't want to document any links at all, you should avoid using the links snippet.

Instead, modify your use of responseFields to ignore the _links field and everything nested beneath it. REST Docs refers to this as a subsection and it can be documented using subsectionWithPath(String) . You want to ignore the _links subsection so you'd do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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