Search code examples
spring-bootspring-mvcspring-hateoashateoasspring-restdocs

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

Solution

  • 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"))