Document an API with Swagger ☕
When creating a web API, good documentation is important for people to learn about your API, and how to use it without any headaches! Swagger provides a specification that generates documentation dynamically based on your code, and allows you to test it in the browser.
I will use it with Spring Boot and Springfox. Springfox allows you to use the spring data repositories as the basis for your documentation, which is important because with Spring Data REST, it is optional to create Controllers and this is usually the basis of the documentation.
Choose a project
If you already have a suitable project, use it. If not, you can grab my Library example.
Maven Dependencies
You need to include these 3 dependencies to include the Swagger and the UI for displaying the documentation.
Configure your project
You just need to add a few annotations in your application class like below.
@EnableSwagger2
is required to enable Swagger 2 support, it registers certain beans into the Spring application context.@Import
imports additional classes into the Spring application context that are needed to automatically create a Swagger documentation from our Spring Data REST repositories.
The Output
When you run Spring Boot, you should be able to view the Swagger documentation of your Spring Data REST API by accessing http://localhost:8080/swagger-ui.html in your browser. The result should look something like the image below.
Customize Documentation
You can customize each aspect of what appears in the UI. You can some methods
to your LibraryApplication
.
Customize Resources
You use the Docket
bean for configuration.
We would like to control the resources (endpoints) shown in the documentation. We do this by:
- Defining a
Docket
bean. - Use
select()
to get an instance ofApiSelectorBuilder
, which provides a way to control the endpoints exposed by Swagger. - You can restrict what Swagger displays by passing parameters to
apis()
andpaths()
. I wanted to restrict by the URL, withpaths()
, you can do this by only including the repositories that are available by a pattern such as “/api/.*”, this then excludes the basic-error-controller and home-controller .
Customize Metadata
To change the metadata that goes on the top of the API page, you use Docket.apiInfo(ApiInfo apiInfo)
.
Embed in our own page
I wanted to have the contents from swagger UI in the body of my own page.
Download (or clone) the Swagger UI repository. You’ll need the following files from the dist folder:
In the <head>
section of your web page, add:
Inside the <body>
, write the code below. The url parameter is pointed to where Swagger generates the JSON of your API, I supply a relative path here.
If you want to customize the display, you can provide parameters to SwaggerUIBundle
, the list is here.
This is the result for my World Cup website. It doesn’t look perfect, but it works well.
Source code
This version of the Libary example had Swagger UI added.
References
- Documenting a Spring Data REST API with Springfox and Swagger
- Setting Up Swagger 2 with a Spring REST API