The blog discusses the importance of documenting RESTful web services and introduces Swagger as a powerful tool for creating, documenting, and visualizing REST APIs. It covers topics such as API evolution, integration with Spring Boot, Maven dependency, and customizing Swagger documentation using annotations.
How to create documentation with Swagger
In recent years, RESTful web services have gained significant popularity and have become the dominant choice in the web services industry. They have surpassed their more complex counterpart, XML or SOAP, and are now the go-to option for API design and development. An API can be thought of as an agreement between the publisher and consumers to ensure effective communication between the two. This agreement, or contract, outlines the expectations and rules that both parties must follow. Just like any other contract, an API must be well documented to function as expected. This documentation should include details such as
the available endpoints,
the types of operations supported by each endpoint,
the expected input format for an operation, and
the format of the response that will be returned for a given request.
To keep up with changing needs, APIs must evolve over time. In this context, proper documentation is not optional, but rather a crucial aspect of the API's offerings, aimed at providing a better user experience. All this is great, but one question arises and that is how should we document for these API's, And the answer is Swagger,
RAML : RESTful API Modeling Language (RAML) is a simple, human-friendly language for describing REST APIs. you can find more information about RAML on https://raml.org/developers/document-your-api
So Swagger is essentially an open-source toolkit that aids in the creation, documentation, and usage of REST APIs, Swagger’s capability to provide API the power to self describe its underlying structure, is what makes it more awesome and tremendously popular. In this article, we will Create a Rest Application and explore the process of documenting Rest APIs and visualizing it through swagger. At first, we will integrate Swagger into a Spring Boot application that performs basic Create, Read, Update, and Delete operations for Employees. If you plan to implement Swagger for APIs you’ve, It’s necessary to first add a maven dependency.
As you can see we have described general Information about our Rest API’s, in function apiInfo(), After this step, when we run the application we will be able to generate JSON representation of our documentation by visiting this link: http://localhost:8080/v2/api-docs
Swagger provides an in built UI tool , so that you can interact with your documentation much easily by visiting this link: http://localhost:8080/swagger-ui/
———————————————————————————————————————
Please note: you can only see above link if Swagger is configured correctly
———————————————————————————————————————
Customizing Swagger Documentation
Swagger provides us the flexibility to customize the generated REST documentation according to our needs. to customize the documentation we will need to use some swagger annotations with your functions in the controller. Some Important Swagger Annotations are as follows :
@ApiModel - Can be used to customize information about Model Class.
@ApiModelProperty - Can be used to provide Extra information about Fields in Model Class.
@ApiResponse - Provides with the Response of particular API
@ApiOperation - Can be used to provides additional information about API
@ApiParam - Can be used to get information about parameters used in API
Incorporating @ApiOperation, @ApiResponse & @ApiParam with your code provides you with following documentation , where we can see customized information is being displayed with associated API
On the other hand if we use @ApiModel & @ApiModelProperty annotations
We will get following output about our Model Class from Swagger
As you can see all properties in model class has description associated with it and also the mandatory field has * associated with it.
Conclusion
Swagger is a powerful tool that compliments your API’s. It offers complete flexibility in automating the documentation process for API’s, and it will be very helpful for the new software engineers that would be joining your team to understand the API structure very clearly and rapidly.
Hello There, I am Sachin Sonone, a software developer who loves tackling complex problems. When I'm not coding, you may often find me engrossed in a game of chess. I thrive on challenging work that pushes my problem-solving and technical skills to the limit.
Want to receive update about our upcoming podcast?