swagger选择生成接口文档

package com.kiiik.config.swagger;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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 org.springframework.ui.ModelMap;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.kiiik.utils.SpringContextUtil;

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;
 
/**
 * Swagger2 UI配置
 * 
 * <pre>
 * 通过访问http://your ip:8090/api/swagger-ui.html查看发布的REST接口;
 * </pre>
 * 
 * @author iechenyb
 * @since JDK1.7
 * @version 2019年10月19日 iechenyb 新建
 */
@Configuration
@PropertySources({ @PropertySource(value = "classpath:swagger2.properties",
ignoreResourceNotFound = true, encoding = "UTF-8") })
@EnableSwagger2
public class Swagger2UIConfig {
	
	@Resource
	SpringContextUtil sct;
    
    /**
     * 获取API标题
     */
    public String title = "xxxx子系统-业务办理系统";
    public  Map<String,String> ignoreController = null;
    /**
     * Swagger2创建Docket的Bean
     * 
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
    	initIngoreControllers();
        return new Docket(DocumentationType.SWAGGER_2)
        	.apiInfo(apiInfo()).ignoredParameterTypes(
        			 HttpServletRequest.class,
        			 HttpServletResponse.class,
        			 HttpSession.class,
        			 ModelMap.class)
        	.select()
            .apis(basePackage("com.kiiik.web"))
            .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  Predicate<RequestHandler> basePackage(final String basePackage) {
        return new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                return declaringClass(input).transform(new MySwaggerUiFunctionHandler(basePackage)).or(true);
            }
        };
    }
  
   /**
    * 初始化过滤掉的controller,即不需要生成接口文档的类。
    */
   public  synchronized void  initIngoreControllers() {
    	ignoreController=new HashMap<>();//自己定义规则,忽略掉某些controller
    	/*ignoreController.put("GenericController",KiiikContants.BLANK);*/
    }
    /**
     * 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
     * @param basePackage 扫描包路径
     * @return Function
     */
  //具体的controller是否需要生成接口文档的逻辑判断。  
  class MySwaggerUiFunctionHandler implements Function<Class<?>, Boolean>{
	  
	  String basePackage;//com.x.y,com.x.z,com.x.w 多个包名,逗号隔开。
	  
	  public  MySwaggerUiFunctionHandler(String basePackage){
		  this.basePackage = basePackage;
	  }

	@Override
	public Boolean apply(Class<?> curController) {//当前类是否需要生成接口文档。
			// 如果生产环境,直接返回false,不生成接口信息
			if (sct.isPro()) {
				return false;
			}
			for (String targetPackage : basePackage.split(",")) {
				boolean isMatch = curController.getPackage().getName().startsWith(targetPackage);
				if (isMatch||!ignoreController.containsKey(curController.getSimpleName())) {//是否是指定基础包路径的子路径
					return true;
				}
			}
			return false;
		}
  }
    
    /**
     * @param input RequestHandler
     * @return Optional
     */
    private  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();
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值