WSO2 Tutorial 1 min

Create Swagger 2 API documentation for REST

How to create Swagger 2 API documentation for REST Services with Spring Boot.

Rob Brouwers
Add the swagger dependencies to the pom.xml

For one of our clients we developed micro REST web services using Spring Boot. Micro web services is a developing trend in industry. You design your component and expose the functions as a web services (just like Service oriented architecture). These components are small, independent and represent a small function . These services can be consumed by applications within the same server or over the network. This gives more flexibility and reduces the tight coupling that we find in many earlier applications.

There are several hundreds of endpoints. Documenting those endpoints and keeping them up to date with new changes is a big task. A small mismatch between the actual implementation and documentation is a cause for problems and confusion.

To fix this, we decided to use SWAGGER 2 for automatic documentation of the REST endpoints. With minimal configurations and without any complexities SWAGGER provides the documentation.

Here are the code snippets for how we managed this in our project.

  1. Add the swagger dependencies to the pom.xmlAdd the swagger dependencies to the pom.xml
  2. Register the below beans in your Spring Configuration file
    1. Enable swagger2 by adding @EnableSwagger2 annotationa. Enable swagger2 by adding @EnableSwagger2 annotation .png
    2. Register swagger 2 related beansa. Register swagger 2 related beans .png
  3. Annotate your model classes Annotate your model classes.png
  4. Annotate your controllersAnnotate your controllers .png

And you are ready with your documentation.

RA API Documentation-1.png

If you have any questions about this blogpost contact us via the comments section of this blog. View also our WSO2 Tutorialswebinars or white papers for more technical information. Need support? We do deliver WSO2 Product Support, WSO2 Development SupportWSO2 Operational Support and WSO2 Training Programs.


Full API lifecycle Management Selection Guide

whitepaper hero
Get it now
We appreciate it
Care to share

Please select one of the social media platforms below to share this pages content with the world