Table of contents
1.
Introduction⭕
2.
What is REST API❓
3.
What is Swagger❓
4.
OpenAPI📌
5.
Generation of Swagger Documentation⭕
6.
Frequently Asked Questions
6.1.
Which languages are used to build REST API?
6.2.
Can I use swagger without paying anything?
6.3.
Is swagger open source?
6.4.
What is pom?
6.5.
What is a docket?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Configuring Auto Generation of Swagger Documentation

Author Akriti Bhan
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction⭕

This blog will discuss configuring auto generation of swagger documentation. We will first start by discussing some basic concepts. The blog will begin with a brief overview of REST API and Swagger. We will then discuss the OpenAPI and its components. Finally, the blog will explain the process of configuring auto generation of swagger documentation in steps.

about

Let us directly start to explore configuring auto generation of swagger documentation now🎯

What is REST API❓

Before directly moving to configuring auto generation of swagger documentation, we must first have a basic understanding of REST API. REST stands for Representational State Transfer. REST API is simply a type of API that uses HTTP requests. The user can access data and use it through these requests. This service is mostly useful in cloud applications. REST API is special as it uses less bandwidth. Some of the data formats supported by REST are as follows

1️⃣multipart/form-data

2️⃣application/xml

3️⃣application/json

What is Swagger❓

swagger

The primary aim of this blog is to learn configuring auto generation of swagger documentation. It is evident that before jumping straightaway into the configuration, we must first understand about swagger.

Swagger is a tool containing a set of rules and particular parameters that help design the REST API. It is instrumental in creating REST API, which the user can easily understand. It makes the REST API more interactive and efficient. It also can be used to produce many client libraries of your API. Swagger provides UI as well as JSON support.

The three main components of swagger are as follows.

1️⃣Swagger editor

It is an editor that can be browsed from the web. The user can write the API specifications in the editor.

2️⃣Swagger UI

The user can use swagger UI to create interactive APIs. The users or developers can interact with the swagger tools using this UI.

3️⃣Swagger codegen

It is open source. The user can create client libraries using this codegen. It is used to simplify the process of designing the API.

OpenAPI📌

openApi

The concept of OpenAPI is essential to understand before configuring auto generation of swagger documentation. OpenAPI is a format for documentation of APIs in the REST APIs. The user can define the whole API through OpenAPI. The parameters that can be specified are as follows.

1️⃣The methods for authentication of API.

2️⃣Licenses used.

3️⃣Information about the API.

4️⃣The endpoints that are available.

Generation of Swagger Documentation⭕

swagger documentation

In this part of the blog, we will proceed with discussing configuring auto generation of swagger documentation. We will discuss the whole process in detail.

1️⃣Go to pom.xml.

2️⃣Add the springfox-swagger2 dependency. The code is as follows.

<dependency>  
<groupId>io.springfox</groupId>  
 <artifactId>springfox-swagger2</artifactId>  
 <version>2.9.2</version>  
</dependency>  
You can also try this code with Online Java Compiler
Run Code

3️⃣Now, add the springfox-swagger-ui dependency. The code for the same is given below.

<dependency>  
<groupId>io.springfox</groupId>  
<artifactId>springfox-swagger-ui</artifactId>  
<version>2.9.2</version>  
</dependency> 
You can also try this code with Online Java Compiler
Run Code
output

4️⃣Create a class and write the given code.

package com.javatpoint.server.main;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import springfox.documentation.spi.DocumentationType;  
import springfox.documentation.spring.web.plugins.Docket;  
import springfox.documentation.swagger2.annotations.EnableSwagger2;  
@Configuration  
@EnableSwagger
public class config  
{  
@Bean  
public Docket api()  
{  
return new Docket(DocumentationType.SWAGGER_2);  
}  
}  
You can also try this code with Online Java Compiler
Run Code

5️⃣Now, run the application.

6️⃣Go to your web browser.

7️⃣Type the following URL:http://localhost:8080/v2/api-docs.

8️⃣Type in the URI - http://localhost:8080/swagger-ui.htmlin your browser itself. The screen will be something like this:

Note: The links given above will give an error if you directly click on them. Ensure to set up the configuration properly by following the steps given above.

swagger output

After expanding the user services, the screen will be something like this:

output

We hope you have understood everything about configuring auto generation of swagger documentation.

Frequently Asked Questions

Which languages are used to build REST API?

Languages like javascript and python are used to design the REST API.

Can I use swagger without paying anything?

Yes, swagger contains public tools that can be accessed by anyone through the GitHub account of swagger.

Is swagger open source?

Yes, swagger is an open-source tool.

What is pom?

Pom stands for Project Object Model. It is a fundamental unit for Maven. It basically is an XML file containing the information about the project.

What is a docket?

It is the primary interface to the swagger MVC framework. It makes the configuration process easier.

Conclusion

In this blog, we discussed about configuring auto generation of swagger documentation. We started by discussing what is swagger and REST API. The blog further explained OpenAPI and its components. Finally, we looked at the steps for configuring auto generation of swagger documentation.

You can refer to other similar articles as well


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available.

Happy Learning Ninja! 🥷

Live masterclass