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

com.luues.swagger.config.Swagger2 Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.swagger.config;

import com.luues.bean.secondary.SystemInfo;
import com.luues.util.TypeConvert;
import com.luues.util.logs.LogUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class Swagger2{

    @Value("${swagger.init:true}")
    private boolean swaggerInit;
    @Value("${swagger.host:null}")
    private String swaggerHost;
    @Value("${swagger.name:接口api}")
    private String swaggerName;
    @Value("${swagger.title:接口api}")
    private String swaggerTitle;
    @Value("${swagger.contact:wdy}")
    private String swaggerContact;
    @Value("${swagger.version:2.9.2}")
    private String swaggerVersion;
    @Value("${swagger.package:wdyNotPackage}")
    private String swaggerPackage;
    @Value("${swagger.description:以下包含所有接口及参数和返回值}")
    private String swaggerDescription;

    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * 函数名,可以随意命名,喜欢什么命名就什么命名
     */
    @Bean
    public Docket createRestApi() {
        if(swaggerInit){
            LogUtil.info("{}", "\n" +
                    "{\n" +
                    "     swagger configuration startup \n" +
                    "     swagger access host --> "+ (TypeConvert.isNull(swaggerHost) ? SystemInfo.getHost() + ":" + SystemInfo.getPort() : swaggerHost) +" \n" +
                    "     swagger version --> "+swaggerVersion+" \n" +
                    "     swagger loading ApiOperation.class \n" +
                    "     swagger config from server at http://"+(TypeConvert.isNull(swaggerHost) ? SystemInfo.getHost() + ":" + SystemInfo.getPort() : swaggerHost)+"/api.html " +
                    "\n}");
            return new Docket(DocumentationType.SWAGGER_2)
                    .host((TypeConvert.isNull(swaggerHost) ? SystemInfo.getHost() + ":" + SystemInfo.getPort() : swaggerHost))
                    .groupName(swaggerName)
                    .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                    .select()
                    //控制暴露出去的路径下的实例
                    //如果某个接口不想暴露,可以使用以下注解
                    //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                    /*.apis(RequestHandlerSelectors.basePackage(swaggerPackage))*/
                    .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    .paths(PathSelectors.any())
                    .build();
        }
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(swaggerName)
                .apiInfo(apiInfo())//调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .select()
                //控制暴露出去的路径下的实例
                //如果某个接口不想暴露,可以使用以下注解
                //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                .apis(RequestHandlerSelectors.basePackage("wdyNotPackage"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title(swaggerTitle)
                //创建人
                .contact(swaggerContact)
                //版本号
                .version(swaggerVersion)
                //描述
                .description(swaggerDescription)
                .build();
    }

    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .deepLinking(true)
                .displayOperationId(false)
                .defaultModelsExpandDepth(1)
                .defaultModelExpandDepth(1)
                .defaultModelRendering(ModelRendering.EXAMPLE)
                .displayRequestDuration(false)
                .docExpansion(DocExpansion.NONE)
                .filter(false)
                .maxDisplayedTags(null)
                .operationsSorter(OperationsSorter.ALPHA)
                .showExtensions(false)
                .tagsSorter(TagsSorter.ALPHA)
                .validatorUrl(null)
                .supportedSubmitMethods(new String[]{"post"})
                .build();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy