RESTful API Documentation With Swagger 2

In this post, I’ll cover how to use Swagger 2 to generate REST API documentation for a Spring Boot project.

Setting up the Swagger 2 in Spring Boot

1.we need the following dependency declaration in our Maven POM.

-----
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>    
<version>2.6.1</version> 
</dependency> 
<dependency> 
   
<groupId>io.springfox</groupId>    
<artifactId>springfox-swagger-ui</artifactId>    
<version>2.6.1</version> 
</dependency>
--------

2.Rest Controller we need implements a set of Swagger 2 REST Endpoint specification.

# customize your resr api documents 
@ApiOperation( value = "List all Product", notes = "Returns a complete list of Product Details") 
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of All Product details" )}) 
@RequestMapping(method=RequestMethod.GET) 
public List<Product> getProducts(){ 
return productJPARepo.findAll(); 
}

3.Configuring Swagger 2 in the Application

# we will create a Docket bean in a Spring Boot configuration to configure Swagger 2 for the application
@Configuration
@EnableSwagger2
public class SwaggerConfig {
       @Bean
       public Docket productApi() {
          return new Docket(DocumentationType.SWAGGER_2)
          .select() .apis(RequestHandlerSelectors.basePackage("com.accenture.api"))
          .paths(PathSelectors.any())
          .build();
      }
}

On pointing your browser to http://localhost:8080/swagger-ui.html, you will see the generated documentation rendered by Swagger UI, like this:

swagger.png

The source code can be found here

4. Centralized Custom API Information in API Gateway.

In API Gateway application follow the above 3 steps to configure the swagger 2 in our project. if You want to centralized all our micro service rest api document in one place we need to add all the micro service end point into  Swagger Resource Provider.

The source code can be found here

 

Leave a comment