springfox swagger-ui多個包路徑掃描匹配的改造支持


使用springfox中的 RequestHandlerSelectors.basePackage("com.xxx") 只能支持單個包路徑的掃描匹配,

如果要想支持多個包路徑的匹配我們需要修改springfox里面的代碼來支持他,現做以下修改來支持多包路徑匹配。

package com.xxx.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
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;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;

/**
 * Swagger2 UI配置
 * 
 * <pre>
 * 通過訪問http://your ip:8090/api/swagger-ui.html查看發布的REST接口;
 * </pre>
 * 
 * @author 許暢
 * @since JDK1.7
 * @version 2017年10月19日 許暢 新建
 */
@Configuration
@PropertySources({ @PropertySource(value = "classpath:swagger2.properties", ignoreResourceNotFound = true, encoding = "UTF-8") })
@EnableSwagger2
public class Swagger2UIConfig {
    
    /**
     * 獲取API標題
     */
    @Value("${swagger2.title}")
    public String title = "Spring Boot中使用Swagger2構建Restful API";
    
    /**
     * Swagger2創建Docket的Bean
     * 
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
            .apis(Swagger2UIConfig.basePackage("com.xxx1,com.xxx2")).paths(PathSelectors.any()).build();
    }
    
    /**
     * Predicate that matches RequestHandler with given base package name for the class of the handler method.
     * This predicate includes all request handlers matching the provided basePackage
     *
     * @param basePackage - base package of the classes
     * @return this
     */
    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return new Predicate<RequestHandler>() {
            
            @Override
            public boolean apply(RequestHandler input) {
                return declaringClass(input).transform(handlerPackage(basePackage)).or(true);
            }
        };
    }
    
    /**
     * 處理包路徑配置規則,支持多路徑掃描匹配以逗號隔開
     * 
     * @param basePackage 掃描包路徑
     * @return Function
     */
    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return new Function<Class<?>, Boolean>() {
            
            @Override
            public Boolean apply(Class<?> input) {
                for (String strPackage : basePackage.split(",")) {
                    boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                    if (isMatch) {
                        return true;
                    }
                }
                return false;
            }
        };
    }
    
    /**
     * @param input RequestHandler
     * @return Optional
     */
    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
    
    /**
     * Swagger2創建該Api的基本信息
     * 
     * @return ApiInfo
     */
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title)
            .description("更多Swagger2配置相關文章請關注:https://springfox.github.io/springfox/docs/current/")
            .termsOfServiceUrl("https://springfox.github.io/springfox/docs/current/").version("1.0").build();
    }
    
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com