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