Saturday, February 17, 2018

Swagger 2 Support for Rest API Documentation

Please follow Rest API Server to see the big picture and GitHub repo details.

Swagger 2 helps in building the documentation for Rest API's. Springfox provides a suite of libraries to support this. To support, Rest API documentation in Spring boot, follow the steps below

Add to Gradle dependencies

compile('io.springfox:springfox-swagger2:2.7.0')
compile('io.springfox:springfox-swagger-ui:2.7.0')

The Swagger dependency helps in documenting the Rest API. The second dependency helps in providing a UI to present the set of API's on browser.

Now define the configuration bean


@Configuration
@EnableSwagger2
public class SwaggerConfig {            
                        
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("com.rms.rest"))              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

Accessing the page at http://localhost:8080/swagger-ui.html will show the details of API's. To handle authorization from the browser one can use plugin like ModHead in chrome which helps in inserting the headers. (Please remember to add appropriate filters to the profile definition of ModHead)


No comments:

Post a Comment