All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kochamcie.Swagger2AutoConfig Maven / Gradle / Ivy

package com.kochamcie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Controller;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;

/**
 * swagger2 auto config implements
 *
 * @author : hama
 * @since created in  2018/5/17
 */
@EnableSwagger2
@ConditionalOnProperty(
        prefix = "swagger",
        name = {"enabled"},
        havingValue = "true"
)
@EnableConfigurationProperties(SwaggerProperties.class)
@Import({
        Swagger2DocumentationConfiguration.class
})
@Controller
public class Swagger2AutoConfig {

    @Autowired
    SwaggerProperties properties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * about your api information
     *
     * @return apiInfo you config
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .termsOfServiceUrl(properties.getTermsOfServiceUrl())
                .contact(contactInfo(properties.getContact()))
                .version(properties.getVersion())
                .build();
    }


    /**
     * convert information to swagger contact
     *
     * @param contact your contact
     * @return swagger contact
     */
    private Contact contactInfo(SwaggerProperties.Contact contact) {
        return new Contact(contact.getName(),
                contact.getUrl(),
                contact.getEmail());

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy