How to use Swagger2

Posted By : Mansi

Aug 28, 2022

INTRODUCTION

Swagger is one of the popular tools used for generating an interactive documentation. It generates an interactive API for the users so that they can understand about the API more quickly.

  • It is helpful when deploying APIs in azure. 
  • It is the standard way of documenting the Standard APIs. 
  • It is primarily used for documenting API.

WHAT IS API??

API stands for Application Programming Interface. It defines how two pieces of software talk to each other. There are several types of APIs, but the swagger specifically deals with the Web API.  The API Definition is a file that describes all the things that we can do with an API. It contains all the requests that we can make to an API. It also describes what request to make and how would response look like for each request. 

There are several advantages of writing an API definition:

  • It allows you to design the API before implementing it. The developers can review the API before writing the code for the API.
  • It also helps in automated testing.
  • It can automatically create a code in several languages.
  • It can also be used to generate the documentation automatically.

HOW TO ENABLE SWAGGER2 IN SPRINGBOOT


Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.


   io.springfox
   springfox-swagger2
   2.7.0


   io.springfox
   springfox-swagger-ui
   2.7.0

The @EnableSwagger2 annotation is used to enable the Swagger2 for your Spring Boot application.


Spring Boot application class will look as shown below −

@SpringBootApplication
@EnableSwagger2
public class SwaggerDemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(SwaggerDemoApplication.class, args);
   }
   @Bean
   public Docket productApi() {
      return new Docket(DocumentationType.SWAGGER_2).select()
         .apis(RequestHandlerSelectors.basePackage("com.abc.demo")).build();
   }
}

 

 

ANATOMY OF A REQUEST 

There are five different parts to be found in the Http request:

1. Method: The method describes the action to be performed. The methods could be POST, PUT, DELETE, GET.
2. URL: It specifies the name on which the action is to be performed.
3. Query parameters
4. Headers: Headers are used to store the information about the request.
5. Body: Body contains the additional data.

 

THE FOLLOWING ARE THE TOOLS INCLUDED IN THE SWAGGER

Swagger Editor: It is a tool that allows us to edit the Open API specifications in YAML inside the browser and can also preview the documentation is real time.
Swagger UI: It is a tool which is a collection of HTML, Javascript, and CSS assets that allows us to generate the beautiful documentation dynamically.
Swagger Codegen: It allows us to generate the API client libraries, server stubs, and documentation automatically.
Swagger core: It consists of java related libraries which are used for creating, consuming and working with API definitions.
Swagger Inspector: It is an API testing tool that allows you to validate your APIs and generate OpenAPI definitions from an existing API.

 

 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

September 8, 2024 at 02:29 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text