Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introducing Spring Auto REST Docs

DZone's Guide to

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.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's 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.

Overview

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 curl request and response body

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.

Improvements

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.

Summary

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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,spring ,rest ,documentation

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}