Swagger 2マルチパッケージスキャン

5661 ワード

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 extends Class>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }

}

 
転載先:https://www.cnblogs.com/wujf/p/11193744.html