1
1
package com .spring4all .swagger ;
2
2
3
3
import com .google .common .base .Predicates ;
4
+ import lombok .RequiredArgsConstructor ;
4
5
import org .springframework .context .annotation .Bean ;
5
6
import org .springframework .context .annotation .Configuration ;
6
7
import springfox .documentation .builders .ApiInfoBuilder ;
7
8
import springfox .documentation .builders .RequestHandlerSelectors ;
9
+ import springfox .documentation .builders .RequestParameterBuilder ;
10
+ import springfox .documentation .schema .ScalarType ;
8
11
import springfox .documentation .service .ApiInfo ;
9
12
import springfox .documentation .service .Contact ;
13
+ import springfox .documentation .service .RequestParameter ;
10
14
import springfox .documentation .spi .DocumentationType ;
11
15
import springfox .documentation .spring .web .plugins .Docket ;
12
16
13
17
import java .util .ArrayList ;
14
18
import java .util .Collections ;
15
19
import java .util .List ;
16
20
import java .util .function .Predicate ;
21
+ import java .util .stream .Collectors ;
22
+
23
+ import static java .util .Collections .singletonList ;
17
24
18
25
/**
19
26
* @author 翟永超
@@ -54,13 +61,36 @@ public Docket createRestApi() {
54
61
55
62
// 需要生成文档的接口目标配置
56
63
Docket docket = builder .select ()
57
- .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ())) // 通过扫描包选择接口
58
- .paths (paths (swaggerProperties )) // 通过路径匹配选择接口
59
- .build ();
64
+ // 通过扫描包选择接口
65
+ .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
66
+ // 通过路径匹配选择接口
67
+ .paths (paths (swaggerProperties ))
68
+ .build ()
69
+ .globalRequestParameters (globalRequestParameters (swaggerProperties ));
60
70
61
71
return docket ;
62
72
}
63
73
74
+
75
+ /**
76
+ * 全局请求参数
77
+ *
78
+ * @param swaggerProperties {@link SwaggerProperties}
79
+ * @return RequestParameter {@link RequestParameter}
80
+ */
81
+ private List <RequestParameter > globalRequestParameters (SwaggerProperties swaggerProperties ) {
82
+ return swaggerProperties .getGlobalOperationParameters ().stream ().map (param -> {
83
+ return new RequestParameterBuilder ()
84
+ .name (param .getName ())
85
+ .description (param .getDescription ())
86
+ .in (param .getParameterType ())
87
+ .required (param .getRequired ())
88
+ .query (q -> q .defaultValue (param .getModelRef ()))
89
+ .query (q -> q .model (m -> m .scalarModel (ScalarType .STRING )))
90
+ .build ();
91
+ }).collect (Collectors .toList ());
92
+ }
93
+
64
94
/**
65
95
* API接口路径选择
66
96
*
@@ -74,13 +104,13 @@ private Predicate paths(SwaggerProperties swaggerProperties) {
74
104
if (swaggerProperties .getBasePath ().isEmpty ()) {
75
105
swaggerProperties .getBasePath ().add ("/**" );
76
106
}
77
- List <com .google .common .base .Predicate <String >> basePath = new ArrayList ();
107
+ List <com .google .common .base .Predicate <String >> basePath = new ArrayList <> ();
78
108
for (String path : swaggerProperties .getBasePath ()) {
79
109
basePath .add (PathSelectors .ant (path ));
80
110
}
81
111
82
112
// exclude-path处理
83
- List <com .google .common .base .Predicate <String >> excludePath = new ArrayList ();
113
+ List <com .google .common .base .Predicate <String >> excludePath = new ArrayList <> ();
84
114
for (String path : swaggerProperties .getExcludePath ()) {
85
115
excludePath .add (PathSelectors .ant (path ));
86
116
}
0 commit comments