Skip to content

Commit dabad03

Browse files
committed
实现根据path规则选择接口生成文档 #200
1 parent c77f239 commit dabad03

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
<version>${version.lombok}</version>
9595
<scope>provided</scope>
9696
</dependency>
97+
98+
<dependency>
99+
<groupId>com.google.guava</groupId>
100+
<artifactId>guava</artifactId>
101+
<version>30.0-jre</version>
102+
</dependency>
103+
97104
</dependencies>
98105

99106
<dependencyManagement>

src/main/java/com/spring4all/swagger/DocketConfiguration.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.spring4all.swagger;
22

3+
import com.google.common.base.Predicates;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import springfox.documentation.builders.ApiInfoBuilder;
@@ -9,7 +10,10 @@
910
import springfox.documentation.spi.DocumentationType;
1011
import springfox.documentation.spring.web.plugins.Docket;
1112

13+
import java.util.ArrayList;
1214
import java.util.Collections;
15+
import java.util.List;
16+
import java.util.function.Predicate;
1317

1418
/**
1519
* @author 翟永超
@@ -51,30 +55,41 @@ public Docket createRestApi() {
5155
// 需要生成文档的接口目标配置
5256
Docket docket = builder.select()
5357
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) // 通过扫描包选择接口
54-
// .paths(paths(swaggerProperties)) // TODO 通过路径匹配选择接口
58+
.paths(paths(swaggerProperties)) // 通过路径匹配选择接口
5559
.build();
5660

5761
return docket;
5862
}
5963

6064
/**
61-
* TODO API接口路径选择
65+
* API接口路径选择
6266
*
6367
* @param swaggerProperties
6468
* @return
6569
*/
66-
// private Predicate paths(SwaggerProperties swaggerProperties) {
67-
// TODO 原来的方式不能用了
68-
// return or(
69-
// regex("/business.*"),
70-
// regex("/some.*"),
71-
// regex("/contacts.*"),
72-
// regex("/pet.*"),
73-
// regex("/springsRestController.*"),
74-
// regex("/test.*")
75-
// );
76-
// return null;
77-
// }
70+
71+
private Predicate paths(SwaggerProperties swaggerProperties) {
72+
// base-path处理
73+
// 当没有配置任何path的时候,解析/**
74+
if (swaggerProperties.getBasePath().isEmpty()) {
75+
swaggerProperties.getBasePath().add("/**");
76+
}
77+
List<com.google.common.base.Predicate<String>> basePath = new ArrayList();
78+
for (String path : swaggerProperties.getBasePath()) {
79+
basePath.add(PathSelectors.ant(path));
80+
}
81+
82+
// exclude-path处理
83+
List<com.google.common.base.Predicate<String>> excludePath = new ArrayList();
84+
for (String path : swaggerProperties.getExcludePath()) {
85+
excludePath.add(PathSelectors.ant(path));
86+
}
87+
88+
return Predicates.and(
89+
Predicates.not(Predicates.or(excludePath)),
90+
Predicates.or(basePath)
91+
);
92+
}
7893

7994
/**
8095
* API文档基本信息
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.spring4all.swagger;
2+
3+
import com.google.common.base.Predicate;
4+
import com.google.common.base.Predicates;
5+
import org.springframework.util.AntPathMatcher;
6+
7+
public class PathSelectors {
8+
private PathSelectors() {
9+
throw new UnsupportedOperationException();
10+
}
11+
12+
/**
13+
* Any path satisfies this condition
14+
*
15+
* @return predicate that is always true
16+
*/
17+
public static Predicate<String> any() {
18+
return Predicates.alwaysTrue();
19+
}
20+
21+
/**
22+
* No path satisfies this condition
23+
*
24+
* @return predicate that is always false
25+
*/
26+
public static Predicate<String> none() {
27+
return Predicates.alwaysFalse();
28+
}
29+
30+
/**
31+
* Predicate that evaluates the supplied regular expression
32+
*
33+
* @param pathRegex - regex
34+
* @return predicate that matches a particular regex
35+
*/
36+
public static Predicate<String> regex(final String pathRegex) {
37+
return new Predicate<String>() {
38+
@Override
39+
public boolean apply(String input) {
40+
return input.matches(pathRegex);
41+
}
42+
};
43+
}
44+
45+
/**
46+
* Predicate that evaluates the supplied ant pattern
47+
*
48+
* @param antPattern - ant Pattern
49+
* @return predicate that matches a particular ant pattern
50+
*/
51+
public static Predicate<String> ant(final String antPattern) {
52+
return new Predicate<String>() {
53+
@Override
54+
public boolean apply(String input) {
55+
AntPathMatcher matcher = new AntPathMatcher();
56+
return matcher.match(antPattern, input);
57+
}
58+
};
59+
}
60+
}

0 commit comments

Comments
 (0)