Git Product home page Git Product logo

spring-restdocs's Introduction

Goal

The primary goal of the project is to make it easier to produce accurate documentation of a RESTful service using AsciiDoctor and Spring's MVC Test Framework. The documentation is intended to be an easily read user guide, akin to GitHub's API documentation for example, rather than dense API documentation produced by tools like Swagger.

Quickstart

The project requires Java 7 or later. It's built with Gradle:

$ ./gradlew build install

Once the main project's built, take a look at one of the two sample projects. Both projects implement a RESTful service for creating tagged notes but have different implementations: rest-notes-spring-hateoas is implemented using Spring MVC and Spring Hateoas while rest-notes-spring-data-rest is implemented using Spring Data REST.

To see the sample project's documentation move into its directory and use Gradle to build the documentation. For example:

$ cd rest-notes-spring-data-rest
$ ./gradlew restDocumentation

Once the build is complete, open on of the generated pieces HTML documentation:

open build/asciidoc/getting-started-guide.html
open build/asciidoc/api-guide.html

Every example request and response in the documentation is auto-generated using custom Spring MVC Test result handlers. This ensures that the examples match the service that they are documenting.

How does it work

There are three main pieces involved in using this project to document your RESTful service.

Gradle plugin

A Gradle plugin is provided. This plugin builds on top of the AsciiDoctor plugin and is responsible for producing the documentation during the build. Assuming you've built and installed the project as described in the quick start, the plugin as configured in your project as follows:

buildscript {
	repositories {
		mavenLocal()
		jcenter()
	}
	dependencies {
		classpath 'org.springframework.restdocs:spring-restdocs-gradle-plugin:0.1.0.BUILD-SNAPSHOT'
	}
}

apply plugin: 'org.springframework.restdocs'

Programatically generated snippets

Spring's MVC Test framework is used to make requests to the service that you are documenting. Any such request wrapped in a call to RestDocumentation.document will produce individual documentation snippets for its request and its response as well as a snippet that contains both its request and its response.

You can configure the scheme, host, and port of any URIs that appear in the documentation snippets:

@Before
public void setUp() {
	this.mockMvc = MockMvcBuilders
			.webAppContextSetup(this.context)
			.apply(new RestDocumentationConfiguration()
					.withScheme("https")
					.withHost("localhost")
					.withPort(8443))
			.build();
}

The default values are http, localhost, and 8080. You can omit the above configuration if these defaults meet your needs.

To document a MockMvc call, wrap it in a call to RestDocumentation.document:

public void getIndex() {
	document("index", this.mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)));
}

The code above will perform a GET request against the index (/) of the service with an accept header indicating that a JSON response is required. It will write the cURL command for the request and the resulting response to files in a directory named index in the project's build/generated-documentation/ directory. Three files will be written:

  • index/request.asciidoc
  • index/response.asciidoc
  • index/request-response.asciidoc

Documentation written in Asciidoc

Producing high-quality, easily readable documentation is difficult and the process is only made harder by trying to write the documentation in an ill-suited format such as Java annotations. This project addresses this by allowing you to write the bulk of your documentation's text as an Asciidoc document. These files should be placed in src/documentation/asciidoc.

To include the programmatically generated snippets in your documentation, you use Asciidoc's include macro. The Gradle plugin provides an attribute, generated, that you can use to reference the directory to which the snippets are written. For example, to include both the request and response snippets described above:

include::{generated}/index/request.asciidoc[]
include::{generated}/index/response.asciidoc[]

Learning more

To learn more, take a look at the accompanying sample projects:

spring-restdocs's People

Contributors

wilkinsona avatar

Stargazers

 avatar krisjin avatar

Watchers

Rossen Stoyanchev avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.