Swagger2多包扫描

package com.xf.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

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
public class Swagger2Config {
    // http://127.0.0.1:8082/swagger-ui.html
    @Bean
    public Docket createRestApi() {
        // return new
        // Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
        // .apis(RequestHandlerSelectors.basePackage("com.xf.controller")).paths(PathSelectors.any()).build();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(basePackage("com.xf.controller;com.xf.PageController")).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder().title("内容:接口文档").description("描述:微信公众号开发")
                .contact(new Contact("XF_AA", null, "[email protected]")).version("版本号:" + 1.0).build();
    }

    public static Predicate basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private static Function, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(";")) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private static Optional> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

}

 

转载于:https://www.cnblogs.com/wujf/p/11193744.html

你可能感兴趣的:(java)