Home » Swagger

Category Archives: Swagger

Swagger

Swagger Overview ::
1. Swagger is an open source software framework backed by a large ecosystem of tools that helps developers design, build, document, and consume RESTful Web services.
2. The Swagger API project was created in 2011 by Tony Tam, Technical co-founder of the dictionary site Wordnik.
3. While most users identify Swagger by the Swagger UI tool, the Swagger toolset includes support for automated documentation, code generation, and test case generation.

What is Swagger UI tool ::
1. Swagger UI is an open source project to visually render documentation for an API defined with the OpenAPI (Swagger) Specification.
2. Using SWagger UI ,One can see all the restful services ,request url,request body and response body.
3. Swagger UI is so simple to use.It can be used by Developer,Tester,Business Analyst,DevOps etc.
4. Swagger UI is very helpful in Development and Testing phase of a project .
5. Swagger UI is the preferred tool now a days as it is used for developing APIs,interacting with APIs and documenting APIs

Adavatages/Usage of Swagger ::
1. Swagger open source tool can be used for developing APIS. It can generate open API document based on code.Swagger follows bottom-up or code-first API Development.API description,sample input is automated using Swagger Tool.
2. Swagger open source tool can be used for interacting with APIs.Using the swagger-codegen project, end users generate client SDKs directly from the Open API document, reducing the need for manual client coding done by developer.
3. Swagger open source tool can be used for documenting APIs.Rest requests can be made directly from the swagger UI .Using swagger UI ,the swagger tool project allows direct interaction with API with HTML-based user interface.Request Body can be manipulated and Response body can be seen in the same UI.Swagger also shows you the error and error description,if any error occurs.

Let us  code a program and see the how to use swagger.
References ::

Spring-Rest-Swagger

If you are new to swagger and do not know where to star.
See this link first and then come back to this page.

Let’s see one example where I am going to create a simple spring boot rest application and will also use swagger.

Step 1 ::
I have used two jars “springfox-swagger2” and “springfox-swagger-ui” with 2.7.0 version.I have added those in my pom.xml.I have also added spring web related dependencies for my spring restful application.The detail pom.xml you can see below.

pom.xml

Step 2 :: I have created a configuration class to use swagger.Use “@EnableSwagger2” to enable swagger.A Docket bean is defined with “SWAGGER_2” DocumentationType.PathSelectors is kept any() to allow all path patterns.RequestHandlerSelectors’ base package is the controller class package name.Controller class will be the class where we will serve rest call mappings.
SwaggerConfig.java

Step 3 :: I have created a model Employee class with id,name and department attributes for RequestBody.
EmployeeModel.java

Step 4 :: I have created the controller class now where the rest call will hit and will generate response.I have used “@Api” and “@ApiOperation” to provide swagger specifications.

Ai1tutorialSwaggerController.java

Step 5 :: Finally a spring boot main class with base package scan for Config and Controller class.
SpringBootRestApp.java

Now you can run your application.
Console OutPut ::

You can now open the URL in your browser http://localhost:8080/swagger-ui.html .For my case the tomcat server port is 8080

Now you can do post request and get response like below.