Springboot整合Swagger2

试用了swagger3,对springboot版本要求高不说,还是存在一些问题的,况且感觉swagger2用着还可以,就用swagger2吧!
springboot整合swagger2三步曲:导包、配置、使用

1. 导包



      
            io.springfox
            springfox-swagger2
            2.9.2
            
                
                    io.swagger
                    swagger-annotations
                
                
                    io.swagger
                    swagger-models
                
            
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
            io.swagger
            swagger-annotations
            1.5.21
        
        
            io.swagger
            swagger-models
            1.5.21
        

2. 添加配置

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

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

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
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.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    // 定义分隔符,配置Swagger多包
    private static final String splitor = ";";
    
    @Bean
    public Docket api() {
        
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                // 指定扫描1个包
//                .apis(RequestHandlerSelectors.basePackage("com.test.controller"))
                // 扫描多包
                .apis(scanBasePackage("com.test.controller"))
                // 只有标记了@Api的类方法才会暴露出给swagger
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                // 只有标记了@ApiOperation的方法才会暴露出给swagger
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .apiInfo(apiInfo());
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
//                .description("Demo接口测试")
                .version("1.0.0-SNAPSHOT")
//                .termsOfServiceUrl("")
                .build();
    }
    
    
    
    /**
     * 方法1:重写basePackage方法,使能够实现多包访问
     */
     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(splitor)) {
                 boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                 if (isMatch) {
                     return true;
                 }
             }
             return false;
         };
     }

     @SuppressWarnings("deprecation")
    private static Optional> declaringClass(RequestHandler input) {
         return Optional.fromNullable(input.declaringClass());
     }
     
     /**
      * 方法2:切割扫描的包生成Predicate
      */
    public static Predicate scanBasePackage(final String basePackage) {
        if(StrUtil.isBlank(basePackage)){
            throw new NullPointerException("basePackage不能为空,多个包扫描使用" + splitor + "分隔");
        }
        String[] controllerPack = basePackage.split(splitor);
        Predicate predicate = null;
        for (int i = controllerPack.length -1; i >= 0 ; i--) {
            String strBasePackage = controllerPack[i];
            if (StrUtil.isNotBlank(strBasePackage)) {
                Predicate tempPredicate = RequestHandlerSelectors.basePackage(strBasePackage);
                predicate = predicate == null ? tempPredicate : Predicates.or(tempPredicate,predicate);
            }
        }
        if(predicate == null) {
            throw new NullPointerException("basePackage配置不正确,多个包扫描使用" + splitor + "分隔");
        }
        return predicate;
    }
}

3. 使用

swagger2使用

你可能感兴趣的:(Springboot整合Swagger2)