DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. 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.

Juraj Misur user avatar by
Juraj Misur
·
Jan. 27, 17 · News
Like (12)
Save
Tweet
Share
27.27K Views

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.

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.

REST Web Protocols Spring Framework Doc (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Authentication Trends to Watch Out for in 2023
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • SAST: How Code Analysis Tools Look for Security Flaws
  • Promises, Thenables, and Lazy-Evaluation: What, Why, How

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: