spring boot 与 swagger 集成

@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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容