Sunday 11 June 2017

Add Swagger to gradle SpringBoot application

Swagger is useful for contracts between controller and UI developer. Also, We can do TESTING the of the controller URLs with the help of swagger UI.

Add below steps to add swagger in graddle springboot App:

1.  gradle dependencies in build.gradle
        compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.4.0'
compile group: 'net.bull.javamelody', name: 'javamelody-core', version: '1.54.0'
2. Create SwaggerConfig class in config package

SwaggerConfig.java:

@Configuration
@EnableSwagger2
public class SwaggerConfig {  
 @Bean
 public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(<packaage>)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("<controller_package>"))     
                .paths(PathSelectors.any())  
                .build();
    }

 private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(<title>)
                .description(<description>)
                .termsOfServiceUrl("https://www.xyz.com/en/terms")
                .license("Xyz License")
                .licenseUrl("https://www.xyz.com")
                .version("1.0")
                .build();
    }
}

There are more option to configure Docket. above are commonly used.

3. To get the UI page for swagger
   Start springBoot project
Hit below URL:
http://<server>:<port>/<project_name>/swagger-ui.html


Image result for swagger-ui.html
Congratullations!! Its done! !!



No comments:

Post a Comment

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...