Do you think IIT Guwahati certified course can help you in your career?
No
Introduction💁
Swagger is a very potent open-source tool used by API developers. The SmartBear Software company developed swagger. Swagger is generally used for describing and developing RESTful APIs. Swagger is used for mentioning the functionality of the API in layman's language for the developers and the testers to understand. The advantage of swagger is that the developers can customise the documentation of swagger.
This blog will briefly discuss how to enhance the swagger documentation using custom annotations.
Steps To Enhance Swagger Documentation with Custom Annotations
This section will show the steps to enhance swagger documentation with custom annotation.
Step 1 - Open the swaggerConfig.java file.
Step 2 - Create a constant with the name DefaultAPI_info of type ApiInfo.
private static final ApiInfo DefaultAPI_info = null;
You can also try this code with Online Java Compiler
Step 3 - Hold the ctrl key and click on the constant type, ApiInfo, which will open the ApiInfo class.
Step 4 - Copy the two constants DEFAULT and DefaultContact from the class or copy the following given code and paste it into the SwaggerConfig.java file. Now rename the constant DEFAULT with DefaultAPI_info.
public static final Contact DefaultContact = new Contact("", "", "");
public static final ApiInfo DefaultAPI_info = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DefaultContact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());
You can also try this code with Online Java Compiler
Step 6: Configure DefaultAPI_info. Please provide all the information in this configuration we want to show in the Swagger documentation.
public static final ApiInfo DefaultAPI_info = new ApiInfo("DemoNinja", "Coding Ninjas documentation of API demo", "1.0", "urn:tos",
DefaultContact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());
You can also try this code with Online Java Compiler
Step 8: Open your browser and type the URL http://localhost:8080/v2/api-docs. It shows the updated API info and contact details in the documentation.
Accepting and Producing XML format
In this step, we will add content negotiation to be more specific. We will accept input in application/XML or application/JSON and then produce the response in application/XML or application/JSON format.
Now we will see the steps for adding content negotiation to our project.
Step 1: Go to the Docket api() in SwaggerConfig.java file and add .produces(DefaultProducesAndConsumes).consumes(DefaultProducesAndConsumes)
return new Docket(DocumentationType.SWAGGER_2).apiInfo(DefaultAPI_info).produces(DefaultProducesAndConsumes).consumes(DefaultProducesAndConsumes);
You can also try this code with Online Java Compiler
Step 3: Now create a HashSet and add two values, application/JSON and application/XML, to it. As we can not directly pass the two values to the HashSet, we are passing a List to the constructor of HashSet.
private static final Set<String> DefaultProducesAndConsumes = new HashSet<String>(Array.asList("application/xml","application/json"));
You can also try this code with Online Java Compiler
It consumes and produces XML and JSON format, as shown in the above output.
We can add more descriptions about the user model, such as the name, which must have at least five characters, and the date of birth, which must be in the past. Let's go through the steps where we will add more description to the user model.
Step 1: Open the User.java file, and just above the class name, add the @ApiModel annotation. And then add the description of the User model.
@ApiModel: This is used to provide additional information about the Swagger models.
@ApiModel(description="All the details of the ninja user")
You can also try this code with Online Java Compiler
Let’s look at the code of the NinjaUser.java file.
package com.codingNinja.server.main.ninjauser;
import java.util.Date;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description="All the details of the ninja user")
public class NinjaUser
{
private Integer id;
@Size(min=5, message="The name of the ninja should have at least five characters")
@ApiModelProperty(notes="The name of the ninja should have at least five characters")
private String NinjaName;
@Past
@ApiModelProperty(notes="The date of birth should be in the past!")
private Date DateOfBirth;
protected NinjaUser()
{
}
public NinjaUser(Integer id, String NinjaName, Date DateOfBirth)
{
super();
this.id = id;
this.NinjaName = NinjaName;
this.DateOfBirth = DateOfBirth;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getNinjaName()
{
return NinjaName;
}
public void setNinjaName(String NinjaName)
{
this.NinjaName = NinjaName;
}
public Date getDob()
{
return DateOfBirth;
}
public void setDob(Date DateOfBirth)
{
this.DateOfBirth = DateOfBirth;
}
@Override
public String toString()
{
return String.format("NinjaUser [id=%s, NinjaName=%s, DateOfBirth=%s]", id, NinjaName, DateOfBirth);
}
}
You can also try this code with Online Java Compiler
Step 5: Open your browser and type the URL http://localhost:8080/v2/api-docs. The description we specified will appear here in the description of the user model.
We hope you have understood everything about enhancing the swagger documentation with Custom Annotations.🙌🥳
Frequently Asked Questions
What is the difference between Swagger and Postman?
Swagger is an API specification used to mention the specifications of the API. In contrast, Postman is an API client used to test the API based on these specifications.
What is the advantage of Swagger?
Swagger is a very potent tool for API developers. The foremost advantage of Swagger is that it provides a page integrated with UI. The UI contains a list of all the APIs, and developers can test the APIs directly using the UI.
Can we use Swagger for free?
A swagger is an open-source tool that can be directly accessed from its GitHub repository. So yes, we can use swagger for free.
What is a REST API?
REST in REST API stands for Representational State Transfer. A REST API or RESTful API is any API that adheres to the constraints set forward by a REST architecture.
What are spring boot annotations?
It is the metadata that provides data about a program. In other words, it provides supplementary information about the program.
Conclusion
This blog covered all the important points regarding enhancing the swagger documentation using custom annotations. We also looked at an example of how to customize the swagger documentation using custom annotations.
Don’t stop here. Check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.