11package com .spring4all .swagger ;
22
33import com .google .common .base .Predicates ;
4+ import lombok .RequiredArgsConstructor ;
45import org .springframework .context .annotation .Bean ;
56import org .springframework .context .annotation .Configuration ;
67import springfox .documentation .builders .ApiInfoBuilder ;
78import springfox .documentation .builders .RequestHandlerSelectors ;
9+ import springfox .documentation .builders .RequestParameterBuilder ;
10+ import springfox .documentation .schema .ScalarType ;
811import springfox .documentation .service .ApiInfo ;
912import springfox .documentation .service .Contact ;
13+ import springfox .documentation .service .RequestParameter ;
1014import springfox .documentation .spi .DocumentationType ;
1115import springfox .documentation .spring .web .plugins .Docket ;
1216
1317import java .util .ArrayList ;
1418import java .util .Collections ;
1519import java .util .List ;
1620import java .util .function .Predicate ;
21+ import java .util .stream .Collectors ;
22+
23+ import static java .util .Collections .singletonList ;
1724
1825/**
1926 * @author 翟永超
@@ -54,13 +61,36 @@ public Docket createRestApi() {
5461
5562 // 需要生成文档的接口目标配置
5663 Docket docket = builder .select ()
57- .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ())) // 通过扫描包选择接口
58- .paths (paths (swaggerProperties )) // 通过路径匹配选择接口
59- .build ();
64+ // 通过扫描包选择接口
65+ .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
66+ // 通过路径匹配选择接口
67+ .paths (paths (swaggerProperties ))
68+ .build ()
69+ .globalRequestParameters (globalRequestParameters (swaggerProperties ));
6070
6171 return docket ;
6272 }
6373
74+
75+ /**
76+ * 全局请求参数
77+ *
78+ * @param swaggerProperties {@link SwaggerProperties}
79+ * @return RequestParameter {@link RequestParameter}
80+ */
81+ private List <RequestParameter > globalRequestParameters (SwaggerProperties swaggerProperties ) {
82+ return swaggerProperties .getGlobalOperationParameters ().stream ().map (param -> {
83+ return new RequestParameterBuilder ()
84+ .name (param .getName ())
85+ .description (param .getDescription ())
86+ .in (param .getParameterType ())
87+ .required (param .getRequired ())
88+ .query (q -> q .defaultValue (param .getModelRef ()))
89+ .query (q -> q .model (m -> m .scalarModel (ScalarType .STRING )))
90+ .build ();
91+ }).collect (Collectors .toList ());
92+ }
93+
6494 /**
6595 * API接口路径选择
6696 *
@@ -74,13 +104,13 @@ private Predicate paths(SwaggerProperties swaggerProperties) {
74104 if (swaggerProperties .getBasePath ().isEmpty ()) {
75105 swaggerProperties .getBasePath ().add ("/**" );
76106 }
77- List <com .google .common .base .Predicate <String >> basePath = new ArrayList ();
107+ List <com .google .common .base .Predicate <String >> basePath = new ArrayList <> ();
78108 for (String path : swaggerProperties .getBasePath ()) {
79109 basePath .add (PathSelectors .ant (path ));
80110 }
81111
82112 // exclude-path处理
83- List <com .google .common .base .Predicate <String >> excludePath = new ArrayList ();
113+ List <com .google .common .base .Predicate <String >> excludePath = new ArrayList <> ();
84114 for (String path : swaggerProperties .getExcludePath ()) {
85115 excludePath .add (PathSelectors .ant (path ));
86116 }
0 commit comments