Spring Boot API Documentation using Swagger

For providing API details to the consumer it’s required to well documents all the API of microservices. Consumers should know the use of a particular API and the data required for making API calls. In spring boot we can well document our API using swagger. You can also test your APIs with swagger

for this tutorial, we are going to implement swagger in our MongoDB spring boot tutorial project. You can check that project here https://gangforcode.com/spring-boot-mongodb-tutorial/. You can also use any spring boot project .

let’s start the development

First add the following dependencies in your pom.xml file

swagger dependencies

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>

After adding these dependencies now we have to write a swagger config class. let’s create a class and add annotations @Configuration @EnableSwagger2 and write the following code

swaggerConfig.java

package com.gangforcode.MongoDBSpringBoot.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
public Docket Docket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.gangforcode.MongoDBSpringBoot"))
.paths(PathSelectors.any())
.build().apiInfo(getInfo());
}

private ApiInfo getInfo() {
ApiInfo apiInfo = new ApiInfo(
"Spring Boot MongoDB Crud Tutoral",
"a spring boot tutorial project to perform crud operation with mongoDB",
"0.0.0",
"Gang For coe",
"https://gangforcode.com",
"Gang For code",
"https://gangforcode.com");
return apiInfo;
}
}

you can add API info according to your service and don’t forget to set your base package inside Docket().

Now run the service and go to http://localhost:8080/swagger-ui/ , here change the port according to your service port

Swagger Ui Output

After running the service and visiting the swagger UI we got the following results.

spring boot swagger implementation
swagger UI implementation

we can also test our API with swagger directly

swagger implementation spring

After giving the required input data for API just click on execute. and after that, we will get the following response with auto-generated student id

spring boot swagger2

for more tutorial you can check https://gangforcode.com/

Leave a Comment