Posted By : Mansi
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.
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:
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.
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.
November 21, 2024 at 11:53 am
Your comment is awaiting moderation.