Press "Enter" to skip to content

Documenting Spring Boot REST services

Documentation is important, everybody knows this, especially when you are the one who needs to use some third party service or library. Without good documentation it’s often a living hell, it’s like walking through a minefield. You carefully take step after step, praying that things won’t blow up. That’s why you HAVE to create documentation, especially for your libraries and REST services. Another important thing about documentation is that you have to update it whenever code changes because the only worse thing than no documentation is wrong documentation, a documentation that is not valid for the current code base.

So if everybody knows about this, it should be ok, right? Wrong. Developers hate creating documentation and they do not do it unless it is really necessary or someone makes them do it – and unfortunately that statement is also true for me. Creating documentation is not bad, but maintaining it, may be hard, especially when you have a critical bug after bug to fix, and deadlines closing in – you just don’t have time to do it.

But there is hope… you can generate it!

Swagger

Swagger is quickly becoming the industry standard for documenting Rest APIs. It is becoming the WSDL of Rest services. Swagger definition consists of simple swagger.json file that describes all endpoints with their parameters, response codes, data types, param requirements, content types and important stuff that you may need to use a service. Also just like in WSDL case, you can generate client and server application stub from the definition file. There are many tools that can help you with that. For example, you can head up to editor.swagger.io upload your definition file there and choose to generate source. You can choose from wide range of languages and technologies, from Javascript, through Bash, Go, Groovy, Objective-C, Android, to plain Java, C# and others. All of this is available in many different technologies and flavors. This lets you really easy jump start your app development.

As a bonus, you can use swagger editor to design your API first, before you write any code.

Add Swagger to Spring Boot app

Spring boot is designed to simplify all possible spring development, designed to automatically do as much as possible. Thanks to all this, adding Swagger to your Spring Boot app is really simple.

First, you have to add springfox swagger plugin to your maven dependencies:

After this, we need to configure it to run. We have to create a Docket bean that hosts all swagger configuration options. You can do it any way you want, I prefer to do it by creating separate spring configuration bean.

And that’s all that is needed for using swagger in its simplest form. Now just run your app, and open http://localhost:8080/swagger-ui.html in your browser. Swagger comes with HTML UI that renders your swagger.json file in really useful form. It presents all endpoints, all their parameters and data model. A really neat thing about this UI is an ability to invoke your services from it. You can use predefined JSON data or input your own and see the results.

If you need it, you can easily define for which endpoints documentation should be available using the paths method argument.

All except /error endpoints:

Only /notes endpoints:

Or combine them any way you want with and() and or() operators.

You can also add author and contact information to your generated docs. Do do this you need to specify ApiInfo property on your Docket configuration.

 

Add documentation to controllers

To generate more meaningful and useful documentation you need to provide some more information than spring-mvc annotation and code. Swagger comes with a small set of annotation that can help you with this.

If you want to name or describe the whole controller, you may use @Api annotation.

For endpoints, you can use @ApiOperation that lets you define content types, response types and some other basic things. Also, you can specify responses, their codes and meaning with @ApiResponses and @ApiResponse. And lastly, there is an @ApiParam for describing input parameters.

The final part of documenting you API, is your request and responses model. Swaggers can handle your classes really well without any annotations, it will show all the fields and their types. But to add some more meaning to it, you have to provide some more information. @ApiModel describes your whole class, gives it name and describes its relationships towards other data types. For annotating field, there is @ApiModelProperty annotation. With this, you can specify its description, its data type, mark field as required or read-only, define allowed values or provide example value.

 

Generate physical docs

Swagger UI and swagger.json file are both great, these two things can greatly improve development time. But something you also need a physical copy of the documentation. You may need a printed version for client or for other third party that want’s to use your service. Json file won’t be a good solution here. But that can also be easily solved. We will use a JUnit test to generate documentation from our swagger.json file served by out application. I know it is a bit of a hack, but it is really simple, it works, and as a side effect, it tests the generation of your API specification, so it’s not so bad really.

To do this, first, we need to add new maven dependency with a test scope, as we need it only for the testing phase.

After that, you can create our test.

As you see, this test is not really complicated. It uses MockMVC to invoke spring rest endpoint (/v2/api-docs) to obtain swagger configuration. In the first test, it just saves the file to src/docs/swagger folder. The second and the third generates documentation in asciidoctor and markdown format respectively to separate folders.

If that’s not enough, we can go further and generate some other formats from asciidoctor docs. To do this, we need to add a new plugin to the maven build.

This example adds additional processing of asciidoctor files in the test phase of a build. We use test phase, wich is a bit weird, because that’s when the asciidoctor file will be generated. We configured here generation of PDF file.

For HTML output, you can add additional execution to the plugin, for example:

 

Conclusion

As you see, there are no excuses now for not creating your rest services documentation. It really can’t be any easier than this. With this setup, you can have many different formats created with each build. It even takes off some burden of maintaining documentation of you, because it will fetch all changes in types, method signatures and changes in spring-mvc annotations. All you need to worry is basically a description of API. The whole working example is available in my GitHub repository https://github.com/lmonkiewicz/spring-swagger-doc.


Also published on Medium.