✨ 添加新特性:Swagger路径排除配置

This commit is contained in:
lishangbu committed 2018-07-25 14:23:34 +08:00
1 parent f37b7e63d2
commit e85ca910c0
3 files changed
+64 -2

No files matched your search

@@ -17,7 +17,10 @@
package com.pig4cloud.pigx.common.swagger.config;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
@@ -43,9 +46,39 @@ import java.util.List;
public class SwaggerAutoConfiguration {
@Bean
public Docket api() {
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties() {
return new SwaggerProperties();
}
private static final String DEFAULT_EXCLUDE_PATH = "/error";
private static final String BASE_PATH = "/**";
@Bean
public Docket api(SwaggerProperties swaggerProperties) {
// base-path处理
// 当没有配置任何path的时候,解析/**
if (swaggerProperties.getBasePath().isEmpty()) {
swaggerProperties.getBasePath().add(BASE_PATH);
}
List<Predicate<String>> basePath = new ArrayList();
for (String path : swaggerProperties.getBasePath()) {
basePath.add(PathSelectors.ant(path));
}
// exclude-path处理
// 当没有任何配置的时候,解析Spring Boot默认的异常处理路径/error
if(swaggerProperties.getExcludePath().isEmpty()){
swaggerProperties.getExcludePath().add(DEFAULT_EXCLUDE_PATH);
}
List<Predicate<String>> excludePath = new ArrayList<>();
for (String path : swaggerProperties.getExcludePath()) {
excludePath.add(PathSelectors.ant(path));
}
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
.apis(RequestHandlerSelectors.any()).paths(Predicates.and(
Predicates.not(Predicates.or(excludePath)),
Predicates.or(basePath)))
.build().securitySchemes(Collections.singletonList(securitySchema()))
.securityContexts(Collections.singletonList(securityContext())).pathMapping("/");
}
@@ -0,0 +1,26 @@
package com.pig4cloud.pigx.common.swagger.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
/**
* SwaggerProperties
*
* @author: lisb
* @date: 2018/7/25 14:00
*/
@Data
@ConfigurationProperties("swagger")
public class SwaggerProperties {
/**
* swagger会解析的url规则
**/
private List<String> basePath = new ArrayList<>();
/**
* 在basePath基础上需要排除的url规则
**/
private List<String> excludePath = new ArrayList<>();
}
@@ -38,3 +38,6 @@ mybatis-plus:
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
# swagger相关配置
swagger:
exclude-path: /actuator/**,/error