Tuesday, September 10, 2019

Swagger2-Integeration

  1. Adding the Maven Dependency
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <properties>
        <swagger.version>2.9.2</swagger.version>
    </properties>
    <dependencies>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
        </dependencies>
  2. create the configuration class to enable swagger2 interface.
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
     
    import javax.servlet.ServletContext;
     
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
     
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
     
    /**
     * @author sunilkumar.gutti
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfig {
        private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = new HashSet<>(Arrays.asList("application/json""application/xml"));
     
        @Bean
        public Docket api(ServletContext servletContext) {
            /*
             * return new Docket(DocumentationType.SWAGGER_2).select()
             * .paths(PathSelectors.regex("/mobile*")).build().apiInfo(apiInfo()).
             * produces(DEFAULT_PRODUCES_AND_CONSUMES)
             * .consumes(DEFAULT_PRODUCES_AND_CONSUMES);
             */
            return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build()
                    .globalOperationParameters(Arrays.asList(new ParameterBuilder().name("Tenant-Ref").description("Teanant Reference (CORE_DB/DEV/QA)")
                            .modelRef(new ModelRef("string")).parameterType("header").required(true).build()))
                    /*
                     * .pathProvider(new RelativePathProvider(servletContext) {
                     *
                     * @Override public String getApplicationBasePath() { return
                     * "/"; } })
                     */.pathMapping("").apiInfo(apiInfo()).produces(DEFAULT_PRODUCES_AND_CONSUMES).consumes(DEFAULT_PRODUCES_AND_CONSUMES);
        }
     
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("CT REST API").description("Control Tower API")
                    .contact(new Contact("Blujay Solutions Team""www.blujaysolutions.com""")).license("Blujay License Version 1.0")
                    .licenseUrl("https://blujaysolutions.com").version("1.0.0").build();
        }
     
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
    }
  3. deploy the latest war file and open the browser and type the URL as below
            E.g: http://localhost:8080/SOA/swagger-ui.html
      4. enter the API-end point as "http://localhost:8080/SOA/mobile" and click ok to see the API documentation as below.

                 
5. Now we can see the rest end-points as below

            



No comments:

Post a Comment