2
2
3
3
import com .google .common .base .Predicate ;
4
4
import com .google .common .base .Predicates ;
5
+ import com .google .common .collect .Lists ;
5
6
import org .springframework .beans .BeansException ;
6
7
import org .springframework .beans .factory .BeanFactory ;
7
8
import org .springframework .beans .factory .BeanFactoryAware ;
10
11
import org .springframework .context .annotation .Bean ;
11
12
import org .springframework .context .annotation .Configuration ;
12
13
import springfox .documentation .builders .ApiInfoBuilder ;
14
+ import springfox .documentation .builders .ParameterBuilder ;
13
15
import springfox .documentation .builders .PathSelectors ;
14
16
import springfox .documentation .builders .RequestHandlerSelectors ;
17
+ import springfox .documentation .schema .ModelRef ;
15
18
import springfox .documentation .service .ApiInfo ;
16
19
import springfox .documentation .service .Contact ;
20
+ import springfox .documentation .service .Parameter ;
17
21
import springfox .documentation .spi .DocumentationType ;
18
22
import springfox .documentation .spring .web .plugins .Docket ;
19
23
20
24
import java .util .ArrayList ;
21
25
import java .util .LinkedList ;
22
26
import java .util .List ;
27
+ import java .util .Set ;
28
+ import java .util .stream .Collectors ;
23
29
24
30
/**
25
31
* @author 翟永超
@@ -43,7 +49,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
43
49
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory ) beanFactory ;
44
50
45
51
// 没有分组
46
- if (swaggerProperties .getDocket ().size () == 0 ) {
52
+ if (swaggerProperties .getDocket ().size () == 0 ) {
47
53
ApiInfo apiInfo = new ApiInfoBuilder ()
48
54
.title (swaggerProperties .getTitle ())
49
55
.description (swaggerProperties .getDescription ())
@@ -58,23 +64,26 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
58
64
59
65
// base-path处理
60
66
// 当没有配置任何path的时候,解析/**
61
- if (swaggerProperties .getBasePath ().isEmpty ()) {
67
+ if (swaggerProperties .getBasePath ().isEmpty ()) {
62
68
swaggerProperties .getBasePath ().add ("/**" );
63
69
}
64
70
List <Predicate <String >> basePath = new ArrayList ();
65
- for (String path : swaggerProperties .getBasePath ()) {
71
+ for (String path : swaggerProperties .getBasePath ()) {
66
72
basePath .add (PathSelectors .ant (path ));
67
73
}
68
74
69
75
// exclude-path处理
70
76
List <Predicate <String >> excludePath = new ArrayList ();
71
- for (String path : swaggerProperties .getExcludePath ()) {
77
+ for (String path : swaggerProperties .getExcludePath ()) {
72
78
excludePath .add (PathSelectors .ant (path ));
73
79
}
74
80
81
+
75
82
Docket docket = new Docket (DocumentationType .SWAGGER_2 )
76
83
.host (swaggerProperties .getHost ())
77
84
.apiInfo (apiInfo )
85
+ .globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
86
+ swaggerProperties .getGlobalOperationParameters ()))
78
87
.select ()
79
88
.apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
80
89
.paths (
@@ -91,7 +100,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
91
100
92
101
// 分组创建
93
102
List <Docket > docketList = new LinkedList <>();
94
- for (String groupName : swaggerProperties .getDocket ().keySet ()) {
103
+ for (String groupName : swaggerProperties .getDocket ().keySet ()) {
95
104
SwaggerProperties .DocketInfo docketInfo = swaggerProperties .getDocket ().get (groupName );
96
105
97
106
ApiInfo apiInfo = new ApiInfoBuilder ()
@@ -102,33 +111,35 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
102
111
.licenseUrl (docketInfo .getLicenseUrl ().isEmpty () ? swaggerProperties .getLicenseUrl () : docketInfo .getLicenseUrl ())
103
112
.contact (
104
113
new Contact (
105
- docketInfo .getContact ().getName ().isEmpty () ? swaggerProperties .getContact ().getName () : docketInfo .getContact ().getName (),
106
- docketInfo .getContact ().getUrl ().isEmpty () ? swaggerProperties .getContact ().getUrl () : docketInfo .getContact ().getUrl (),
107
- docketInfo .getContact ().getEmail ().isEmpty () ? swaggerProperties .getContact ().getEmail () : docketInfo .getContact ().getEmail ()
114
+ docketInfo .getContact ().getName ().isEmpty () ? swaggerProperties .getContact ().getName () : docketInfo .getContact ().getName (),
115
+ docketInfo .getContact ().getUrl ().isEmpty () ? swaggerProperties .getContact ().getUrl () : docketInfo .getContact ().getUrl (),
116
+ docketInfo .getContact ().getEmail ().isEmpty () ? swaggerProperties .getContact ().getEmail () : docketInfo .getContact ().getEmail ()
108
117
)
109
118
)
110
119
.termsOfServiceUrl (docketInfo .getTermsOfServiceUrl ().isEmpty () ? swaggerProperties .getTermsOfServiceUrl () : docketInfo .getTermsOfServiceUrl ())
111
120
.build ();
112
121
113
122
// base-path处理
114
123
// 当没有配置任何path的时候,解析/**
115
- if (docketInfo .getBasePath ().isEmpty ()) {
124
+ if (docketInfo .getBasePath ().isEmpty ()) {
116
125
docketInfo .getBasePath ().add ("/**" );
117
126
}
118
127
List <Predicate <String >> basePath = new ArrayList ();
119
- for (String path : docketInfo .getBasePath ()) {
128
+ for (String path : docketInfo .getBasePath ()) {
120
129
basePath .add (PathSelectors .ant (path ));
121
130
}
122
131
123
132
// exclude-path处理
124
133
List <Predicate <String >> excludePath = new ArrayList ();
125
- for (String path : docketInfo .getExcludePath ()) {
134
+ for (String path : docketInfo .getExcludePath ()) {
126
135
excludePath .add (PathSelectors .ant (path ));
127
136
}
128
137
129
138
Docket docket = new Docket (DocumentationType .SWAGGER_2 )
130
139
.host (swaggerProperties .getHost ())
131
140
.apiInfo (apiInfo )
141
+ .globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
142
+ docketInfo .getGlobalOperationParameters ()))
132
143
.groupName (groupName )
133
144
.select ()
134
145
.apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
@@ -150,4 +161,49 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
150
161
public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
151
162
this .beanFactory = beanFactory ;
152
163
}
164
+
165
+ private List <Parameter > buildGlobalOperationParametersFromSwaggerProperties (
166
+ List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ) {
167
+ List <Parameter > parameters = Lists .newArrayList ();
168
+ for (SwaggerProperties .GlobalOperationParameter globalOperationParameter : globalOperationParameters ) {
169
+ parameters .add (new ParameterBuilder ()
170
+ .name (globalOperationParameter .getName ())
171
+ .description (globalOperationParameter .getDescription ())
172
+ .modelRef (new ModelRef (globalOperationParameter .getModelRef ()))
173
+ .parameterType (globalOperationParameter .getParameterType ())
174
+ .required (Boolean .parseBoolean (globalOperationParameter .getRequired ()))
175
+ .build ());
176
+ }
177
+ return parameters ;
178
+ }
179
+
180
+ /**
181
+ * 局部参数按照name覆盖局部参数
182
+ *
183
+ * @param globalOperationParameters
184
+ * @param docketOperationParameters
185
+ * @return
186
+ */
187
+ private List <Parameter > assemblyGlobalOperationParameters (
188
+ List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ,
189
+ List <SwaggerProperties .GlobalOperationParameter > docketOperationParameters ) {
190
+
191
+ if (docketOperationParameters == null || docketOperationParameters .isEmpty ()) {
192
+ return buildGlobalOperationParametersFromSwaggerProperties (globalOperationParameters );
193
+ }
194
+
195
+ Set <String > docketNames = docketOperationParameters .stream ()
196
+ .map (SwaggerProperties .GlobalOperationParameter ::getName )
197
+ .collect (Collectors .toSet ());
198
+
199
+ List <SwaggerProperties .GlobalOperationParameter > resultOperationParameters = Lists .newArrayList ();
200
+
201
+ for (SwaggerProperties .GlobalOperationParameter parameter : globalOperationParameters ) {
202
+ if (!docketNames .contains (parameter .getName ())) {
203
+ resultOperationParameters .add (parameter );
204
+ }
205
+ }
206
+ resultOperationParameters .addAll (docketOperationParameters );
207
+ return buildGlobalOperationParametersFromSwaggerProperties (resultOperationParameters );
208
+ }
153
209
}
0 commit comments