Introducing Spring Auto REST Docs
Introducing Spring Auto REST Docs
Documentation is essential, and Spring Auto REST Docs can help. Using as many standardized tools as possible, the makers have enabled automatic API documentation for Spring MVC.
Join the DZone community and get the full member experience.Join For Free
Almost a year ago, my colleague Florian Benz wrote an article about our journey to better API documentation. Since then, we worked continuously on updates and improvements, but it was mostly inside our internal projects. But today, after open-sourcing the project and adding many cleanups and enhancements, we are proud to present the first official release of Spring Auto REST Docs — the fully automatic API documentation for Spring MVC.
Spring REST Docs project is fairly popular among spring developers. By plugging into MockMvc-based tests for API, it enables us to capture various artefacts (called snippets) about the test itself. The snippets can be as simple as showing the API call as a curl request, capturing the request and response as raw HTTP data or creating an entire table of request and response JSON fields. Then the actual documentation is just putting it all together using AsciiDoc or Markdown — essentially providing a huge amount of work for free. However, there is still some overhead which our project tries to tackle.
Example of auto-generated curl request and response body snippets.
Spring REST Docs
The main pain point of using plain Spring REST Docs is manual documentation of request and response JSON fields. They are documented inside the code, at the end of MockMvc expectations, and thus pollute the code quite significantly if the objects are non-trivial or repeated across many endpoints.
Tedious parameter documentation.
The highlight of the Spring Auto REST Docs extension lies in automated recognition and documentation of request and response parameters. By reusing the same Jackson deserializer mechanics as real request processing, it's able to extract JSON field names and types from request and response classes.
It also recognizes most common bean validation annotations present on the class fields and merges them with their descriptions so everything is in one place. Constraints that determine the optionality of a field such as the @NotNull annotation or @RequestParam's required flag are translated directly into a separate Optional column.
All of this is flushed into a compact parameter table and linked to the main document.
Example of response class and corresponding HTML output.
Now the question is, "Where does the actual field description come from?" Look no further than JavaDoc! Instead of using custom annotations like Swagger or polluting tests to describe what a field represents, we decided to stick as much as possible to the standard. It's pretty much the project philosophy, so we chose the only non-intrusive and well-known way of documenting code — JavaDoc. Therefore, any JavaDoc comment which documents a field, method parameter or return value is recognized and plugged into its proper place in the resulting table. For this to work, the project uses a custom JavaDoc extension (doclet), which goes through all project classes and extracts the JavaDoc of each class into a file for later processing.
This essentially describes the core idea of this extension — introspect as much about the code as possible, using standard features of Java and Spring, without the need for any additional manual work on the developer side.
There are some small fine-tunings and enhancements like support for Page response type, custom validation groups and various helper snippets, e.g. to document authorization. Check it all out, together with how to build and integrate the extension into your project in the documentation.
Opinions expressed by DZone contributors are their own.