Documenting an API has several benefits. It helps stakeholders understand the API itself, and it speeds up development by giving clear instruction. Most projects use OpenAPI specification as the format. I will explain how to integrate OpenAPI specification into a Spring Boot project, specifically Tabunganku.
OpenAPI
OpenAPI specification, originally named Swagger specification, is a standardized format for documenting RESTful APIs, outlining details such as request/response formats, endpoints, and more. I use springdoc-openapi library for generating a documentation using OpenAPI specification. To use this library, the application must have annotations that will be parsed later by the library.
Implementation
Import Library
To use springdoc-openapi
, we have to add a new dependency in pom.xml
:
|
|
Implementation in Java
After importing it, we can use the new annotations to define our operations. I use them in the interface classes.
|
|
In the ICategoriesController
class above, I use several annotations: @Tag
, @Operation
, and @ApiResponses
. @Tag
is used to describe a class. In this case, the name of this controller is Category
. To describe the operation itself, I use @Operation
and @ApiResponses
.
What is the difference between those two? I use @Operation
to write general information about what the operation does, and @ApiResponses
to define possible outputs of the operation. In the example above, I write summary, description, and tags of an operation using @Operation
annotation. On the other hand, I use @ApiResponses
to map two possible outputs of the operation usong @ApiResponse
annotation. The first @ApiResponse
is the output when the function succeeds and returning AddCategoryResponse
object. The other one is when the function encounters an error and return a status code 400, along with an ErrorBody
schema.
What is ErrorBody
schema mentioned above then ? It is a custom schema to wrap validation errors. If you haven’t already tried it, every validation error will lead to a list that explains the reason for the input being invalid. This is what the ErrorBody
entails.
To create this custom schema in the document, we define it in a configuration class. In DocsConfiguration
class, I create an OpenAPI
bean and create a new Schema object named errorSchema
, which is a map with the key errors
and a list of error strings as the value.
|
|
Insert @OpenAPIDefinition
annotation in the main class (TabungankuApplication
) to flesh out information about the application in the generated document.
|
|
In the code above, I include additional data about server endpoint, application name, application description, and application version.
Configuration File
To view the documentation page, we have to add a new property in the configuration file. Add this in the application.properties
file:
springdoc.api-docs.enabled=true
Run the backend server, and go to the endpoint /swagger-ui/index.html
. For instance, if the backend server’s base URL is http://localhost:8080
, go to http://localhost:8080/swagger-ui/index.html
to view the API document.