org.networking.config.SwaggerConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.networking.config.SwaggerConfig.java

Source

package org.networking.config;

import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements EnvironmentAware {

    public static final String DEFAULT_INCLUDE_PATTERNS = "/controller/.*";

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
    }

    @Bean
    public Docket swaggerSpringfoxDocket() {
        StopWatch watch = new StopWatch();
        watch.start();
        Docket swaggerSpringMvcPlugin = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .genericModelSubstitutes(ResponseEntity.class).select()
                .paths(PathSelectors.regex(DEFAULT_INCLUDE_PATTERNS)) // and by paths
                .build();
        watch.stop();
        return swaggerSpringMvcPlugin;
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(propertyResolver.getProperty("title"), propertyResolver.getProperty("description"),
                propertyResolver.getProperty("version"), propertyResolver.getProperty("termsOfServiceUrl"),
                propertyResolver.getProperty("contact"), propertyResolver.getProperty("license"),
                propertyResolver.getProperty("licenseUrl"));
    }
}