This version is still in development and is not considered stable yet. For the latest stable version, please use Spring REST Docs 4.0.0!

Reusing Snippets

It is common for an API that is being documented to have some features that are common across several of its resources. To avoid repetition when documenting such resources, you can reuse a Snippet configured with the common elements.

First, create the Snippet that describes the common elements. The following example shows how to do so:

import org.springframework.restdocs.hypermedia.LinksSnippet;

import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;

public final class CommonSnippets {

	private CommonSnippets() {

	}

	public static final LinksSnippet pagingLinks = links(
			linkWithRel("first").optional().description("The first page of results"),
			linkWithRel("last").optional().description("The last page of results"),
			linkWithRel("next").optional().description("The next page of results"),
			linkWithRel("prev").optional().description("The previous page of results"));

}

Second, use this snippet and add further descriptors that are resource-specific. The following examples show how to do so:

  • MockMvc

  • WebTestClient

import org.junit.jupiter.api.Test;

import org.springframework.http.MediaType;
import org.springframework.restdocs.docs.howto.reusingsnippets.CommonSnippets;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class ReusingSnippets {

	// Fields

	private MockMvc mockMvc;


	@Test
	void test() throws Exception {
		this.mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk())
			.andDo(document("example", CommonSnippets.pagingLinks.and((1)
					linkWithRel("alpha").description("Link to the alpha resource"),
					linkWithRel("bravo").description("Link to the bravo resource"))));
	}

}
1 Reuse the pagingLinks Snippet, calling and to add descriptors that are specific to the resource that is being documented.
import org.junit.jupiter.api.Test;

import org.springframework.http.MediaType;
import org.springframework.restdocs.docs.howto.reusingsnippets.CommonSnippets;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;

public class ReusingSnippets {

	// Fields

	private WebTestClient webTestClient;


	@Test
	void test() {
		this.webTestClient.get()
			.uri("/")
			.accept(MediaType.APPLICATION_JSON)
			.exchange()
			.expectStatus()
			.isOk()
			.expectBody()
			.consumeWith(document("example", CommonSnippets.pagingLinks.and((1)
					linkWithRel("alpha").description("Link to the alpha resource"),
					linkWithRel("bravo").description("Link to the bravo resource"))));
	}

}
1 Reuse the pagingLinks Snippet, calling and to add descriptors that are specific to the resource that is being documented.

The result of the example is that links with rel values of first, last, next, previous, alpha, and bravo are all documented.