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 getPathRules() {
        Iterable> 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("接口约定");
        return sb.toString();
    }
}

你可能感兴趣的:(spring boot 与 swagger 集成)