How to configure swagger in Spring Boot Application?

Swagger is a tool which is used to develop APIs, interact with APIs, document APIs and test APIs. The main application of swagger is to document and test the APIs. 

Now the question is why  we need to document APIs?

So when you develop an API, you need to provide some information(end-points, response codes, request-response structure, error conditions to handle etc) about the API to the consumers who are going to use that API.  The easiest way to do this is to prepare a document and  write all the details of the API and provide that document to the consumers. To avoid manual  APIs documentation we can use swagger which creates APIs documents automatically.

There are four steps to document your spring boot application.

  1. Getting the Swagger 2 Spring dependency
  2.  Enabling Swagger in your code.  
  3.  Configuring Swagger (UI).
  4.  Customize swagger by using Docket
  1. Getting the Swagger 2 Spring dependency:

Following dependency can be used for swagger configuration. Add this dependency  in your pom.xml file.

<!– https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 –>

<dependency>

  <groupId>io.springfox</groupId>

  <artifactId>springfox-swagger2</artifactId>

  <version>2.9.2</version>

</dependency>

For latest version use following link:

https://mvnrepository.com/artifact/io.springfox/springfox-swagger2/2.9.2

2. Enabling Swagger in your code

To enable swagger in your code you need to use annotation @EnableSwagger2

@SpringBootApplication
@EnableSwagger2
public class SwaggerApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
     new SpringApplicationBuilder(SwaggerApplication.class)
           .sources(SwaggerApplication.class)
           .run(args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
     return application
           .sources(SwaggerApplication.class);
  }

}

Now run your project and hit the following url in your postman and you will get your API documentation in JSON format.

http://localhost:8080/v2/api-docs

3. Configuring Swagger (UI).

    Use following dependency in your pom.xml file for configuring swagger    UI. 

<!– https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui –>

<dependency>  

 <groupId>io.springfox</groupId>

  <artifactId>springfox-swagger-ui</artifactId>

  <version>2.9.2</version>

</dependency>

For latest version use following link:

https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui/2.0.2

now run your project and hit the following url in your browser.

http://localhost:8080/swagger-ui.html#/

4. Customize swagger by using Docket

  We can customize swagger by  adding  detail as annotations to API using Docket Object. Add the following configuration class in your base package and customize your swagger configuration according to the requirements.

@Configuration
@EnableSwagger2
public class Swagger2Config {

   @Bean
      public Docket api() {
          return new Docket(DocumentationType.SWAGGER_2).select()
              .apis(RequestHandlerSelectors
                  .basePackage("com.api"))   // base package of your project
              .paths(PathSelectors.regex("/.*")) // base path of your project
              .build().apiInfo(apiEndPointsInfo());
      }
// by following method you can add API details in your swagger
      private ApiInfo apiEndPointsInfo() {
          return new ApiInfoBuilder().title(" REST API")
              .description( REST API Docs")
              .license("Apache 2.0")
              .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
              .version("1.0.0")
              .build();
      }
}

Leave a Reply