7
7
import org .springframework .beans .factory .BeanFactory ;
8
8
import org .springframework .beans .factory .BeanFactoryAware ;
9
9
import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
10
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
10
11
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
11
12
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
12
13
import org .springframework .context .annotation .Bean ;
13
14
import org .springframework .context .annotation .Configuration ;
14
15
import org .springframework .context .annotation .Import ;
15
- import springfox .documentation .builders .ApiInfoBuilder ;
16
- import springfox .documentation .builders .ParameterBuilder ;
17
- import springfox .documentation .builders .PathSelectors ;
18
- import springfox .documentation .builders .RequestHandlerSelectors ;
16
+ import org .springframework .util .StringUtils ;
17
+ import org .springframework .web .bind .annotation .RequestMethod ;
18
+ import springfox .documentation .builders .*;
19
19
import springfox .documentation .schema .ModelRef ;
20
20
import springfox .documentation .service .ApiInfo ;
21
21
import springfox .documentation .service .Contact ;
22
22
import springfox .documentation .service .Parameter ;
23
+ import springfox .documentation .service .ResponseMessage ;
23
24
import springfox .documentation .spi .DocumentationType ;
24
25
import springfox .documentation .spring .web .plugins .Docket ;
26
+ import springfox .documentation .swagger .web .UiConfiguration ;
25
27
26
28
import java .util .*;
27
29
import java .util .stream .Collectors ;
@@ -45,8 +47,22 @@ public SwaggerProperties swaggerProperties() {
45
47
return new SwaggerProperties ();
46
48
}
47
49
50
+ @ Bean
51
+ public UiConfiguration uiConfiguration (SwaggerProperties swaggerProperties ) {
52
+ return new UiConfiguration (
53
+ swaggerProperties .getUiConfig ().getValidatorUrl (),// url
54
+ swaggerProperties .getUiConfig ().getDocExpansion (), // docExpansion => none | list
55
+ swaggerProperties .getUiConfig ().getApiSorter (), // apiSorter => alpha
56
+ swaggerProperties .getUiConfig ().getDefaultModelRendering (), // defaultModelRendering => schema
57
+ swaggerProperties .getUiConfig ().getSubmitMethods ().split ("," ),
58
+ swaggerProperties .getUiConfig ().getJsonEditor (), // enableJsonEditor => true | false
59
+ swaggerProperties .getUiConfig ().getShowRequestHeaders (), // showRequestHeaders => true | false
60
+ swaggerProperties .getUiConfig ().getRequestTimeout ()); // requestTimeout => in milliseconds, defaults to null (uses jquery xh timeout)
61
+ }
62
+
48
63
@ Bean
49
64
@ ConditionalOnMissingBean
65
+ @ ConditionalOnBean (UiConfiguration .class )
50
66
@ ConditionalOnProperty (name = "swagger.enabled" , matchIfMissing = true )
51
67
public List <Docket > createRestApi (SwaggerProperties swaggerProperties ) {
52
68
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory ) beanFactory ;
@@ -82,20 +98,30 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
82
98
excludePath .add (PathSelectors .ant (path ));
83
99
}
84
100
85
- Docket docket = new Docket (DocumentationType .SWAGGER_2 )
101
+ Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
86
102
.host (swaggerProperties .getHost ())
87
103
.apiInfo (apiInfo )
88
104
.globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
89
- swaggerProperties .getGlobalOperationParameters ()))
90
- .select ()
105
+ swaggerProperties .getGlobalOperationParameters ()));
106
+
107
+ // 全局响应消息
108
+ if (!swaggerProperties .getApplyDefaultResponseMessages ()) {
109
+ buildGlobalResponseMessage (swaggerProperties , docketForBuilder );
110
+ }
111
+
112
+ Docket docket = docketForBuilder .select ()
91
113
.apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
92
114
.paths (
93
115
Predicates .and (
94
116
Predicates .not (Predicates .or (excludePath )),
95
117
Predicates .or (basePath )
96
118
)
97
- )
98
- .build ();
119
+ ).build ();
120
+
121
+ /** ignoredParameterTypes **/
122
+ Class [] array = new Class [swaggerProperties .getIgnoredParameterTypes ().size ()];
123
+ Class [] ignoredParameterTypes = swaggerProperties .getIgnoredParameterTypes ().toArray (array );
124
+ docket .ignoredParameterTypes (ignoredParameterTypes );
99
125
100
126
configurableBeanFactory .registerSingleton ("defaultDocket" , docket );
101
127
docketList .add (docket );
@@ -138,12 +164,18 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
138
164
excludePath .add (PathSelectors .ant (path ));
139
165
}
140
166
141
- Docket docket = new Docket (DocumentationType .SWAGGER_2 )
167
+ Docket docketForBuilder = new Docket (DocumentationType .SWAGGER_2 )
142
168
.host (swaggerProperties .getHost ())
143
169
.apiInfo (apiInfo )
144
170
.globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
145
- docketInfo .getGlobalOperationParameters ()))
146
- .groupName (groupName )
171
+ docketInfo .getGlobalOperationParameters ()));
172
+
173
+ // 全局响应消息
174
+ if (!swaggerProperties .getApplyDefaultResponseMessages ()) {
175
+ buildGlobalResponseMessage (swaggerProperties , docketForBuilder );
176
+ }
177
+
178
+ Docket docket = docketForBuilder .groupName (groupName )
147
179
.select ()
148
180
.apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
149
181
.paths (
@@ -154,17 +186,24 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
154
186
)
155
187
.build ();
156
188
189
+ /** ignoredParameterTypes **/
190
+ Class [] array = new Class [docketInfo .getIgnoredParameterTypes ().size ()];
191
+ Class [] ignoredParameterTypes = docketInfo .getIgnoredParameterTypes ().toArray (array );
192
+ docket .ignoredParameterTypes (ignoredParameterTypes );
193
+
157
194
configurableBeanFactory .registerSingleton (groupName , docket );
158
195
docketList .add (docket );
159
196
}
160
197
return docketList ;
161
198
}
162
199
200
+
163
201
@ Override
164
202
public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
165
203
this .beanFactory = beanFactory ;
166
204
}
167
205
206
+
168
207
private List <Parameter > buildGlobalOperationParametersFromSwaggerProperties (
169
208
List <SwaggerProperties .GlobalOperationParameter > globalOperationParameters ) {
170
209
List <Parameter > parameters = Lists .newArrayList ();
@@ -216,4 +255,57 @@ private List<Parameter> assemblyGlobalOperationParameters(
216
255
resultOperationParameters .addAll (docketOperationParameters );
217
256
return buildGlobalOperationParametersFromSwaggerProperties (resultOperationParameters );
218
257
}
258
+
259
+ /**
260
+ * 设置全局响应消息
261
+ *
262
+ * @param swaggerProperties 支持 POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE
263
+ * @param docketForBuilder
264
+ */
265
+ private void buildGlobalResponseMessage (SwaggerProperties swaggerProperties , Docket docketForBuilder ) {
266
+
267
+ SwaggerProperties .GlobalResponseMessage globalResponseMessages =
268
+ swaggerProperties .getGlobalResponseMessage ();
269
+
270
+ // POST,GET,PUT,PATCH,DELETE,HEAD,OPTIONS,TRACE 响应消息体
271
+ List <ResponseMessage > postResponseMessages = getResponseMessageList (globalResponseMessages .getPost ());
272
+ List <ResponseMessage > getResponseMessages = getResponseMessageList (globalResponseMessages .getGet ());
273
+ List <ResponseMessage > putResponseMessages = getResponseMessageList (globalResponseMessages .getPut ());
274
+ List <ResponseMessage > patchResponseMessages = getResponseMessageList (globalResponseMessages .getPatch ());
275
+ List <ResponseMessage > deleteResponseMessages = getResponseMessageList (globalResponseMessages .getDelete ());
276
+ List <ResponseMessage > headResponseMessages = getResponseMessageList (globalResponseMessages .getHead ());
277
+ List <ResponseMessage > optionsResponseMessages = getResponseMessageList (globalResponseMessages .getOptions ());
278
+ List <ResponseMessage > trackResponseMessages = getResponseMessageList (globalResponseMessages .getTrace ());
279
+
280
+ docketForBuilder .useDefaultResponseMessages (swaggerProperties .getApplyDefaultResponseMessages ())
281
+ .globalResponseMessage (RequestMethod .POST , postResponseMessages )
282
+ .globalResponseMessage (RequestMethod .GET , getResponseMessages )
283
+ .globalResponseMessage (RequestMethod .PUT , putResponseMessages )
284
+ .globalResponseMessage (RequestMethod .PATCH , patchResponseMessages )
285
+ .globalResponseMessage (RequestMethod .DELETE , deleteResponseMessages )
286
+ .globalResponseMessage (RequestMethod .HEAD , headResponseMessages )
287
+ .globalResponseMessage (RequestMethod .OPTIONS , optionsResponseMessages )
288
+ .globalResponseMessage (RequestMethod .TRACE , trackResponseMessages );
289
+ }
290
+
291
+ /**
292
+ * 获取返回消息体列表
293
+ *
294
+ * @param globalResponseMessageBodyList
295
+ * @return
296
+ */
297
+ private List <ResponseMessage > getResponseMessageList (List <SwaggerProperties .GlobalResponseMessageBody > globalResponseMessageBodyList ) {
298
+ List <ResponseMessage > responseMessages = new ArrayList <>();
299
+ for (SwaggerProperties .GlobalResponseMessageBody globalResponseMessageBody : globalResponseMessageBodyList ) {
300
+ ResponseMessageBuilder responseMessageBuilder = new ResponseMessageBuilder ();
301
+ responseMessageBuilder .code (globalResponseMessageBody .getCode ()).message (globalResponseMessageBody .getMessage ());
302
+
303
+ if (!StringUtils .isEmpty (globalResponseMessageBody .getModelRef ())) {
304
+ responseMessageBuilder .responseModel (new ModelRef (globalResponseMessageBody .getModelRef ()));
305
+ }
306
+ responseMessages .add (responseMessageBuilder .build ());
307
+ }
308
+
309
+ return responseMessages ;
310
+ }
219
311
}
0 commit comments