Flat Preloader Icon

Spring REST Docs

Spring REST Docs is a testing and documentation tool within the Spring Framework ecosystem that helps you create accurate and up-to-date documentation for your RESTful APIs. It’s particularly useful for documenting Spring-based applications that follow the principles of HATEOAS (Hypertext as the Engine of Application State) and adhere to RESTful best practices. Spring REST Docs allows you to generate documentation by writing tests for your API endpoints.

Here are the key features and components of Spring REST Docs:

  • Documentation Generation: Spring REST Docs generates documentation in various formats, including plain text, HTML, and AsciiDoc. You can choose the format that best suits your project’s needs.
  • Test-Driven Documentation: Documentation is generated based on tests that you write for your API endpoints. This ensures that your documentation is always up-to-date and reflects the actual behavior of your API.
  • Template Formats: Spring REST Docs uses template formats for documentation, such as Asciidoctor and Markdown. This allows you to customize the appearance and content of your API documentation.
  • Snippets: Snippets are code fragments extracted from your test code that are used to generate documentation. They capture information about request and response structures, including headers, JSON payloads, and more.
  • Spring MVC Test Framework Integration: Spring REST Docs integrates seamlessly with the Spring MVC Test framework, making it easy to write tests for your RESTful API endpoints.
  • Documentation DSL: Spring REST Docs provides a concise DSL (Domain-Specific Language) for writing documentation snippets within your tests. This DSL includes methods for documenting request and response attributes, such as headers, path parameters, query parameters, and request/response bodies.
  • Here’s a simplified example of how Spring REST Docs works:

    = "build/generated-snippets")
    public class ApiDocumentation 
     @Autowired private 
     WebApplicationContext context;
     private MockMvc mockMvc;
    public void setUp()
    this.mockMvc = MockMvcBuilders
        public void exampleDocumentation() 
        throws Exception
        ("/api/resource/{id}", 1))
        ("The ID of the resource to retrieve")
        ("The ID of the resource"),
        ("The name of the resource")

    In this example, a Spring MVC test is written to document the /api/resource/{id} endpoint. The andDo(document(...)) method is used to specify the documentation format and describe the request and response fields.

    After running the tests, Spring REST Docs generates documentation in the specified output directory (build/generated-snippets) in the chosen format (e.g., AsciiDoc, HTML). You can then include this documentation in your project’s documentation set or publish it for API consumers.

    Spring REST Docs is a powerful tool for keeping your API documentation up-to-date and ensuring that it accurately reflects your API’s behavior. It encourages a test-driven approach to documentation, which is especially beneficial for complex APIs that may change over time.

    Share on: