Swagger2系列二 多模块分工开发时配置

 

核心配置

每个模块调用此配置分别命名和分组,下文会举例说明:


import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;


@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Models {
    //    http://localhost:8088/swagger-ui.html     原路径
    //    http://localhost:8088/doc.html     原路径
    // 配置swagger2核心配置 docket
    public static Docket docket(String moduleCode, String moduleName, String... basePackage){
        return  new Docket(DocumentationType.SWAGGER_2)//制定api类型为swagger2类型
                .apiInfo(apiInfo(moduleName)) //定义api文档汇总信息
                .groupName(moduleCode)
                .select().apis(basePackage(basePackage))//扫描哪个包下面
                .paths(PathSelectors.any())//所有接口
                .build();
    }


    private static ApiInfo apiInfo(String moduleName) {


        return new ApiInfoBuilder()
                .title(moduleName)  //标题
                .contact(new Contact("yitian","https//www.yitian.com","[email protected]"))//联系方式
                .description("专为吃货准备的Api文档")//描述
                .version("0.0.1")//版本
                .termsOfServiceUrl("https//www.yitian.com")//网址
                .build();


    }


    /**
     * 声明基础包
     *
     * @param basePackage 基础包路径
     * @return
     */
    public static Predicate basePackage(final String[] basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }


    /**
     * 校验基础包
     *
     * @param basePackage 基础包路径
     * @return
     */
    private static Function, Boolean> handlerPackage(final String[] basePackage) {
        return input -> {
            for (String strPackage : basePackage) {
                //校验包的前缀存在
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }


    /**
     * 检验基础包实例
     *
     * @param requestHandler 请求处理类
     * @return
     */
    @SuppressWarnings("deprecation")
    private static Optional> declaringClass(RequestHandler requestHandler) {
        return Optional.fromNullable(requestHandler.declaringClass());
    }


}

举例Core模块:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * @Description:
 * @Date: 2:44 下午 2021/4/23
 * @Author: renyitian
 */
@Configuration
public class CoreSwagger2Config {


    /**
     * 新增Core模块
     *
     * @return
     */
    @Bean
    public Docket coreApi() {
        String moduleCode = "core";
        String moduleName = "核心模块";
        String[] basePackage = { "com.yitian.testcontroller" };
        return Swagger2Models.docket(moduleCode, moduleName, basePackage);
    }
}

order模块

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * @Description:
 * @Date: 2:42 下午 2021/4/23
 * @Author: renyitian
 */
@Configuration
public class OrderSwagger2Config {


    /**
     * 新增order模块
     *
     * @return
     */
    @Bean
    public Docket shopApi() {
        String moduleCode = "order";
        String moduleName = "订单模块";
        String[] basePackage = {"com.yitian.controller"};
        return Swagger2Models.docket(moduleCode, moduleName, basePackage);
    }


}

效果展示:

在系列三实现完成之后的效果

Swagger2系列二 多模块分工开发时配置_第1张图片

Swagger2系列二 多模块分工开发时配置_第2张图片

你可能感兴趣的:(Swagger2,spring,java)