22
33import com .google .common .base .Predicate ;
44import com .google .common .base .Predicates ;
5+ import com .google .common .collect .Lists ;
56import org .springframework .beans .BeansException ;
67import org .springframework .beans .factory .BeanFactory ;
78import org .springframework .beans .factory .BeanFactoryAware ;
89import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
910import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
11+ import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
1012import org .springframework .context .annotation .Bean ;
1113import org .springframework .context .annotation .Configuration ;
14+ import org .springframework .context .annotation .Import ;
1215import springfox .documentation .builders .ApiInfoBuilder ;
16+ import springfox .documentation .builders .ParameterBuilder ;
1317import springfox .documentation .builders .PathSelectors ;
1418import springfox .documentation .builders .RequestHandlerSelectors ;
19+ import springfox .documentation .schema .ModelRef ;
1520import springfox .documentation .service .ApiInfo ;
1621import springfox .documentation .service .Contact ;
22+ import springfox .documentation .service .Parameter ;
1723import springfox .documentation .spi .DocumentationType ;
1824import springfox .documentation .spring .web .plugins .Docket ;
1925
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 ;
2328
2429/**
2530 * @author 翟永超
26- * Create date :2017/8/7.
31+ * Create date:2017/8/7.
2732 * My blog: http://blog.didispace.com
2833 */
2934@ Configuration
35+ @ Import ({
36+ Swagger2Configuration .class
37+ })
3038public class SwaggerAutoConfiguration implements BeanFactoryAware {
3139
3240 private BeanFactory beanFactory ;
@@ -39,11 +47,12 @@ public SwaggerProperties swaggerProperties() {
3947
4048 @ Bean
4149 @ ConditionalOnMissingBean
50+ @ ConditionalOnProperty (name = "swagger.enabled" , matchIfMissing = true )
4251 public List <Docket > createRestApi (SwaggerProperties swaggerProperties ) {
4352 ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory ) beanFactory ;
4453
4554 // 没有分组
46- if (swaggerProperties .getDocket ().size () == 0 ) {
55+ if (swaggerProperties .getDocket ().size () == 0 ) {
4756 ApiInfo apiInfo = new ApiInfoBuilder ()
4857 .title (swaggerProperties .getTitle ())
4958 .description (swaggerProperties .getDescription ())
@@ -58,23 +67,26 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
5867
5968 // base-path处理
6069 // 当没有配置任何path的时候,解析/**
61- if (swaggerProperties .getBasePath ().isEmpty ()) {
70+ if (swaggerProperties .getBasePath ().isEmpty ()) {
6271 swaggerProperties .getBasePath ().add ("/**" );
6372 }
6473 List <Predicate <String >> basePath = new ArrayList ();
65- for (String path : swaggerProperties .getBasePath ()) {
74+ for (String path : swaggerProperties .getBasePath ()) {
6675 basePath .add (PathSelectors .ant (path ));
6776 }
6877
6978 // exclude-path处理
7079 List <Predicate <String >> excludePath = new ArrayList ();
71- for (String path : swaggerProperties .getExcludePath ()) {
80+ for (String path : swaggerProperties .getExcludePath ()) {
7281 excludePath .add (PathSelectors .ant (path ));
7382 }
7483
84+
7585 Docket docket = new Docket (DocumentationType .SWAGGER_2 )
7686 .host (swaggerProperties .getHost ())
7787 .apiInfo (apiInfo )
88+ .globalOperationParameters (buildGlobalOperationParametersFromSwaggerProperties (
89+ swaggerProperties .getGlobalOperationParameters ()))
7890 .select ()
7991 .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
8092 .paths (
@@ -91,7 +103,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
91103
92104 // 分组创建
93105 List <Docket > docketList = new LinkedList <>();
94- for (String groupName : swaggerProperties .getDocket ().keySet ()) {
106+ for (String groupName : swaggerProperties .getDocket ().keySet ()) {
95107 SwaggerProperties .DocketInfo docketInfo = swaggerProperties .getDocket ().get (groupName );
96108
97109 ApiInfo apiInfo = new ApiInfoBuilder ()
@@ -102,33 +114,35 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
102114 .licenseUrl (docketInfo .getLicenseUrl ().isEmpty () ? swaggerProperties .getLicenseUrl () : docketInfo .getLicenseUrl ())
103115 .contact (
104116 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 ()
108120 )
109121 )
110122 .termsOfServiceUrl (docketInfo .getTermsOfServiceUrl ().isEmpty () ? swaggerProperties .getTermsOfServiceUrl () : docketInfo .getTermsOfServiceUrl ())
111123 .build ();
112124
113125 // base-path处理
114126 // 当没有配置任何path的时候,解析/**
115- if (docketInfo .getBasePath ().isEmpty ()) {
127+ if (docketInfo .getBasePath ().isEmpty ()) {
116128 docketInfo .getBasePath ().add ("/**" );
117129 }
118130 List <Predicate <String >> basePath = new ArrayList ();
119- for (String path : docketInfo .getBasePath ()) {
131+ for (String path : docketInfo .getBasePath ()) {
120132 basePath .add (PathSelectors .ant (path ));
121133 }
122134
123135 // exclude-path处理
124136 List <Predicate <String >> excludePath = new ArrayList ();
125- for (String path : docketInfo .getExcludePath ()) {
137+ for (String path : docketInfo .getExcludePath ()) {
126138 excludePath .add (PathSelectors .ant (path ));
127139 }
128140
129141 Docket docket = new Docket (DocumentationType .SWAGGER_2 )
130142 .host (swaggerProperties .getHost ())
131143 .apiInfo (apiInfo )
144+ .globalOperationParameters (assemblyGlobalOperationParameters (swaggerProperties .getGlobalOperationParameters (),
145+ docketInfo .getGlobalOperationParameters ()))
132146 .groupName (groupName )
133147 .select ()
134148 .apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
@@ -150,4 +164,56 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
150164 public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
151165 this .beanFactory = beanFactory ;
152166 }
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+ }
153219}
0 commit comments