zoukankan      html  css  js  c++  java
  • 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, "921252375@qq.com")).version("版本号:" + 1.0).build();
        }
    
        public static Predicate<RequestHandler> basePackage(final String basePackage) {
            return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
        }
    
        private static Function<Class<?>, 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());
        }
    
    }
  • 相关阅读:
    虚拟机黑屏
    ngnix随笔三
    ngnix随笔二
    ngnix随笔一
    HTML(初级)笔记
    学习Java注解
    常用工具类与commons 类库
    SpringBoot缓存使用
    LeetCode:98.验证二叉搜索树
    剑指Offer:面试题07.重建二叉树
  • 原文地址:https://www.cnblogs.com/wujf/p/11193744.html
Copyright © 2011-2022 走看看