美文网首页
spring boot 与 swagger 集成

spring boot 与 swagger 集成

作者: 牧呈 | 来源:发表于2018-05-15 22:14 被阅读0次
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.regex("/api/.*"))
                    .build()
                    .apiInfo(getApiInfo());
        }
    
        @Bean
        public Docket restfulApi() {
            return new Docket(DocumentationType.SWAGGER_2).groupName("RestfulApi")
                    .genericModelSubstitutes(ResponseEntity.class)
                    .useDefaultResponseMessages(true)
                    .forCodeGeneration(false)
                    .select().paths(getPathRules())
                    .build().apiInfo(getApiInfo());
        }
        
        /**
         * 设置过滤规则 这里的过滤规则支持正则匹配
         *
         * @return
         */
        private Predicate<String> getPathRules() {
            Iterable<Predicate<String>> ptn_excludes = Arrays.stream(excludePaths.split(","))
                    .map(p -> ant(p))::iterator;
            return not(or(ptn_excludes));
        }
    
        private ApiInfo getApiInfo() {
            ApiInfo apiInfo = new ApiInfo("系统 - REST API", getDescribe(),
                    null, null, (Contact) null, null, null);
            return apiInfo;
        }
    
      private String getDescribe() {
            StringBuffer sb = new StringBuffer();
            sb.append("<b><font size=\"4\" color=\"red\">接口约定</font>");
            return sb.toString();
        }
    }
    

    相关文章

      网友评论

          本文标题:spring boot 与 swagger 集成

          本文链接:https://www.haomeiwen.com/subject/pnxudftx.html