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


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


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;

public class swaggerConfig {
public Docket Docket() {
return new Docket(DocumentationType.SWAGGER_2)

private ApiInfo getInfo() {
ApiInfo apiInfo = new ApiInfo(
"Spring Boot MongoDB Crud Tutoral",
"a spring boot tutorial project to perform crud operation with mongoDB",
"Gang For coe",
"Gang For code",
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