用Stream API写Swagger配置

分享一下平时写得比较规范的代码片段

@Data
@Configuration
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties{
    private Boolean enabled = true;
    private List basePath = Collections.emptyList();
    private List excludePath = Collections.emptyList();
    private List basePackage = Collections.emptyList();

    private String title = "标题";
    private String description = "描述";
    private String version = "版本";
    private String termsOfServiceUrl = "http://www.bocloud.com/terms";
    private Contact contact = new Contact("Author", "http://www.bocloud.com", "[email protected]");
    private String license = "协议";
    private String licenseUrl = "http://www.bocloud.com/license";

    public ApiInfo apiInfo(){

        return new ApiInfo(title, description, version, termsOfServiceUrl, contact.toString(), license, licenseUrl);
    }

    public Predicate excludePath(){
        return excludePath.stream()
                .map(PathSelectors::regex)
                .map(Predicates::not)
                .reduce(Predicates::or)
                .orElseGet(Predicates::alwaysFalse);
    }

    public Predicate basePath(){
        return basePath.stream()
                .map(PathSelectors::regex)
                .reduce(Predicates::or)
                .orElseGet(Predicates::alwaysFalse);
    }

    public Optional> apis(){
        return basePackage.stream()
                .map(RequestHandlerSelectors::basePackage)
                .reduce(Predicates::or);
    }

    public Predicate paths(){
        return Predicates.or(excludePath(), basePath());
    }
}

你可能感兴趣的:(用Stream API写Swagger配置)