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