Should I Use Spring REST Docs or OpenAPI?

Should I Use Spring REST Docs or OpenAPI?

1 Comment

Should I Use Spring REST Docs or OpenAPI?

Recently, I was asked which is better to use. Spring REST Docs or OpenAPI.

From the perspective of generating documentation for your APIs, you may think these two options are effectively the same thing.

But, it’s not exactly an apples to apples comparison.

These are significantly different options.

Spring REST Docs is a tool which helps you create API documentation from your controller tests. By default, the output of Spring REST Docs is plain text via Asciidoctor. This output is used to generate API documentation.

In contrast, OpenAPI is a formal specification for APIs. The specification is backed by a JSON schema, which used to fully describe APIs.

OpenAPI Specifications are written in JSON or YAML. The specification can then be programmatically parsed to produce rich API documentation.

When you create an API, having complete and accurate documentation is very important.

Both Spring REST Docs and OpenAPI can produce accurate documentation for your APIs.

Let’s take a closer look at how each accomplishes this.

Spring REST Docs

The official documentation for Spring REST Docs is here.

Spring Boot Test Configuration

Setting up a Spring Boot test to use Spring REST Docs is fairly simple.


public class BeerControllerTest {

In the above, you need to annotate the test with  @ExtendWith(RestDocumentationExtension.class) which is a JUnit 5 extension for Spring REST Docs. And  @AutoConfigureRestDocs which will provide you an auto configured instance of Spring MockMVC for the test.

Example JUnit 5 Test

Here is an example of a test using Spring REST Docs.

public void getBeer() throws Exception {

    ConstrainedFields fields = new ConstrainedFields(BeerDto.class);

    mockMvc.perform(get("/api/v1/beer/{beerId}", validBeer.getId().toString()).accept(MediaType.APPLICATION_JSON))
            .andExpect(jsonPath("$.id", is(validBeer.getId().toString())))
            .andExpect(jsonPath("$.beerName", is("Beer1")))
                    pathParameters (
                            parameterWithName("beerId").description("UUID of desired beer to get.")
                            fields.withPath("id").description("Id of Beer").type(UUID.class),
                            fields.withPath("createdDate").description("Date Created").type(OffsetDateTime.class),
                            fields.withPath("lastUpdatedDate").description("Date Updated").type(OffsetDateTime.class),
                            fields.withPath("beerName").description("Beer Name"),
                            fields.withPath("beerStyle").description("Beer Style"),
                            fields.withPath("upc").description("UPC of Beer")

The test above will create several asciidoc snippets.

Here is an example of a generated snippet.


|`+class java.util.UUID+`
|Id of Beer

|`+class java.time.OffsetDateTime+`
|Date Created

|`+class java.time.OffsetDateTime+`
|Date Updated

|Beer Name

|Beer Style

|UPC of Beer


Through asciidoc configuration, these snippets can be configured into a rich HTML document. (See Spring documentation for details.)

This document can be published for your API users.

For a complete working example, please see this GitHub repo (note branch!).


The approach of OpenAPI is significantly different.

OpenAPI is a formal specification. You can see details of the 3.0.2 version here.

This is significantly different from Spring REST Docs.

The OpenAPI Specification is a very popular open source project with broad support. It is a technology standard, and is not Spring specific.

The Get Beer operation tested above could be defined in OpenAPI as:

        -   $ref: "#/components/parameters/BeerId"
        -   $ref: '#/components/parameters/ShowInventoryOnHand'
        description: Get Beer by Id
            - Beer Service
                description: Get Beer by id Response
                            $ref: '#/components/schemas/Beer'
                description: Not Found

This is just the snippet of the operation. The complete OpenAPI specification can be found here.

If you’re new to OpenAPI, I have a complete course called OpenAPI: Beginner to Guru to quickly get you up to speed writing your own specifications.

Generating User Documentation with OpenAPI

OpenAPI specifications are written in JSON or YAML.

This is great for computers. Not so much for people.

OpenAPI has a very large community of Open Source tools. These are tools which can read specifications to produce an output. The output can be documentation, client SDKs, Server stubbs, Mock Servers, Testing tools, and much more.

There is a whole segment of tools for generating end user API documentation from OpenAPI Specifications.

ReDoc API Documentation Example

One of my favorite documentation generators is ReDoc. ReDoc uses React to provide a rich single page application experience.

Here is a screenshot example of ReDoc.

redoc openapi example

You can see a complete, working example hosted on GitHub Pages here.

Testing with OpenAPI

You may be thinking a clear gap between Spring REST Docs and OpenAPI is testing.

With Spring REST Docs, the documentation is driven from controller tests. This gives you confidence that the generated documentation is correct.

By using Atlassian’s Swagger Request validator, we can validate every request / response against an OpenAPI specification.

Here is an example JUnit 5 test with Spring MockMVC.

public static final String OAC_SPEC = "";

 void getBeerById() throws Exception {
     DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
     given(beerService.findBeerById(any(UUID.class), anyBoolean())).willReturn(validReturnBeer);

     mockMvc.perform(get("/api/v1/beer/" + UUID.randomUUID()).accept(MediaType.APPLICATION_JSON))
             .andExpect(jsonPath("$.beerName", is("Beer1")))

Here you can see how easy it is to assert your controller test matches the OpenAPI specification.

The last line:


Is the code which invokes validations from Atlassian’s Swagger Request Validator.

The complete example is available here in GitHub.


I’ve used Spring REST Docs for some time. It does get the job of creating API documentation done.

However, writing tests becomes very verbose when you wish to have detailed documentation.

The asciidoc process always feels like a bit of a kludge to me.

Yes it works. But, can get overly complicated.

Also, the output of Spring REST Docs also feels closed. You get a HTML document. It is unique to your APIs and configuration.

In contrast, with OpenAPI, you are starting with a widely accepted standard and a thriving open source community.

From an OpenAPI Specification you can:

  • Create End user API Documentation
  • Generate client side code in over 50 languages
  • Generate server side code in over 40 languages
  • Create Mock servers
  • Import directly into Postman for API testing
  • Define Consumer Driven Contracts
  • Generate tests
  • Integrate specification validation into your tests
  • And more!

Which to Use?

If you’re at a Java shop, and writing API documentation for internal use, Spring REST Docs is fine.

I suggest strongly considering using OpenAPI if:

  • You have public facing APIs
  • Are in a large corporation with a diverse technology base
  • Are using a microservice architecture

Learn more about writing OpenAPI Specifications here!

About jt

    You May Also Like

    One comment

    1. January 23, 2021 at 3:45 pm

      This is an awesome article and thank your putting it together
      I am trying to do similar thing with RestAssuredMockMvc but haven’t been successful. The swagger-request-validator library has a separate module for the RestAssured using filters but RestAssuredMockMvc doesn’t have filters.
      Do you know if there is another way I can make this work?


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.