Skip to content

Commit f76eef3

Browse files
author
程序猿DD-翟永超
authored
Merge pull request #16 from dyc87112/1.4.0-xiaohou
1.4.0 增加全局参数配置
2 parents 3f72fce + 3dd02c0 commit f76eef3

File tree

5 files changed

+136
-11
lines changed

5 files changed

+136
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020

2121
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2222
hs_err_pid*
23+
.idea/
24+
spring-boot-starter-swagger.iml
25+
target/

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: java
2+
3+
jdk:
4+
- oraclejdk8
5+
6+
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ swagger.contact.email=dyc87112@qq.com
6565
swagger.base-package=com.didispace
6666
swagger.base-path=/**
6767
swagger.exclude-path=/error, /ops/**
68+
swagger.globalOperationParameters[0].name=name one
69+
swagger.globalOperationParameters[0].description=some description one
70+
swagger.globalOperationParameters[0].modelRef=string
71+
swagger.globalOperationParameters[0].parameterType=header
72+
swagger.globalOperationParameters[0].required=true
73+
swagger.globalOperationParameters[1].name=name two
74+
swagger.globalOperationParameters[1].description=some description two
75+
swagger.globalOperationParameters[1].modelRef=string
76+
swagger.globalOperationParameters[1].parameterType=body
77+
swagger.globalOperationParameters[1].required=false
6878
```
6979

7080
## 配置说明
@@ -85,8 +95,14 @@ swagger.exclude-path=/error, /ops/**
8595
- swagger.base-path=需要处理的基础URL规则,默认:/**
8696
- swagger.exclude-path=需要排除的URL规则,默认:空
8797
- swagger.host=文档的host信息,默认:空
98+
- swagger.globalOperationParameters[0].name=参数名
99+
- swagger.globalOperationParameters[0].description=描述信息
100+
- swagger.globalOperationParameters[0].modelRef=指定参数类型
101+
- swagger.globalOperationParameters[0].parameterType=指定参数存放位置,可选header,query,path,body.form
102+
- swagger.globalOperationParameters[0].required=指定参数是否必传,true,false
88103
```
89104

105+
90106
> host属性从1.3.0.RELEASE开始支持
91107
92108
### Path规则说明
@@ -129,10 +145,19 @@ swagger.exclude-path=/ops/**, /error
129145
- swagger.docket.<name>.base-package=swagger扫描的基础包,默认:全扫描
130146
- swagger.docket.<name>.base-path=需要处理的基础URL规则,默认:/**
131147
- swagger.docket.<name>.exclude-path=需要排除的URL规则,默认:空
148+
- swagger.docket.<name>.name=参数名
149+
- swagger.docket.<name>.modelRef=指定参数类型
150+
- swagger.docket.<name>.parameterType=指定参数存放位置,可选header,query,path,body.form
151+
- swagger.docket.<name>.required=true=指定参数是否必传,true,false
152+
- swagger.docket.<name>.globalOperationParameters[0].name=参数名
153+
- swagger.docket.<name>.globalOperationParameters[0].description=描述信息
154+
- swagger.docket.<name>.globalOperationParameters[0].modelRef=指定参数存放位置,可选header,query,path,body.form
155+
- swagger.docket.<name>.globalOperationParameters[0].parameterType=指定参数是否必传,true,false
132156
```
133157

134158
说明:`<name>`为swagger文档的分组名称,同一个项目中可以配置多个分组,用来划分不同的API文档。
135159

160+
136161
**分组配置示例**
137162

138163
```properties
@@ -144,13 +169,21 @@ swagger.docket.aaa.contact.name=zhaiyongchao
144169
swagger.docket.aaa.contact.url=http://spring4all.com/
145170
swagger.docket.aaa.contact.email=didi@potatomato.club
146171
swagger.docket.aaa.excludePath=/ops/**
172+
swagger.docket.aaa.globalOperationParameters[0].name=name three
173+
swagger.docket.aaa.globalOperationParameters[0].description=some description three override
174+
swagger.docket.aaa.globalOperationParameters[0].modelRef=string
175+
swagger.docket.aaa.globalOperationParameters[0].parameterType=header
147176

148177
swagger.docket.bbb.title=group-bbb
149178
swagger.docket.bbb.basePackage=com.yonghui
179+
180+
150181
```
151182

152183
说明:默认配置与分组配置可以一起使用。在分组配置中没有配置的内容将使用默认配置替代,所以默认配置可以作为分组配置公共部分属性的配置。
153184

185+
`swagger.docket.aaa.globalOperationParameters[0].name` 会覆盖同名的全局配置
186+
154187
### JSR-303校验注解支持
155188

156189
支持对JSR-303校验注解的展示,如下图所示:

src/main/java/com/didispace/swagger/SwaggerAutoConfiguration.java

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Predicate;
44
import com.google.common.base.Predicates;
5+
import com.google.common.collect.Lists;
56
import org.springframework.beans.BeansException;
67
import org.springframework.beans.factory.BeanFactory;
78
import org.springframework.beans.factory.BeanFactoryAware;
@@ -10,16 +11,21 @@
1011
import org.springframework.context.annotation.Bean;
1112
import org.springframework.context.annotation.Configuration;
1213
import springfox.documentation.builders.ApiInfoBuilder;
14+
import springfox.documentation.builders.ParameterBuilder;
1315
import springfox.documentation.builders.PathSelectors;
1416
import springfox.documentation.builders.RequestHandlerSelectors;
17+
import springfox.documentation.schema.ModelRef;
1518
import springfox.documentation.service.ApiInfo;
1619
import springfox.documentation.service.Contact;
20+
import springfox.documentation.service.Parameter;
1721
import springfox.documentation.spi.DocumentationType;
1822
import springfox.documentation.spring.web.plugins.Docket;
1923

2024
import java.util.ArrayList;
2125
import java.util.LinkedList;
2226
import java.util.List;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
2329

2430
/**
2531
* @author 翟永超
@@ -43,7 +49,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
4349
ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
4450

4551
// 没有分组
46-
if(swaggerProperties.getDocket().size() == 0) {
52+
if (swaggerProperties.getDocket().size() == 0) {
4753
ApiInfo apiInfo = new ApiInfoBuilder()
4854
.title(swaggerProperties.getTitle())
4955
.description(swaggerProperties.getDescription())
@@ -58,23 +64,26 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
5864

5965
// base-path处理
6066
// 当没有配置任何path的时候,解析/**
61-
if(swaggerProperties.getBasePath().isEmpty()) {
67+
if (swaggerProperties.getBasePath().isEmpty()) {
6268
swaggerProperties.getBasePath().add("/**");
6369
}
6470
List<Predicate<String>> basePath = new ArrayList();
65-
for(String path : swaggerProperties.getBasePath()) {
71+
for (String path : swaggerProperties.getBasePath()) {
6672
basePath.add(PathSelectors.ant(path));
6773
}
6874

6975
// exclude-path处理
7076
List<Predicate<String>> excludePath = new ArrayList();
71-
for(String path : swaggerProperties.getExcludePath()) {
77+
for (String path : swaggerProperties.getExcludePath()) {
7278
excludePath.add(PathSelectors.ant(path));
7379
}
7480

81+
7582
Docket docket = new Docket(DocumentationType.SWAGGER_2)
7683
.host(swaggerProperties.getHost())
7784
.apiInfo(apiInfo)
85+
.globalOperationParameters(buildGlobalOperationParametersFromSwaggerProperties(
86+
swaggerProperties.getGlobalOperationParameters()))
7887
.select()
7988
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
8089
.paths(
@@ -91,7 +100,7 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
91100

92101
// 分组创建
93102
List<Docket> docketList = new LinkedList<>();
94-
for(String groupName : swaggerProperties.getDocket().keySet()) {
103+
for (String groupName : swaggerProperties.getDocket().keySet()) {
95104
SwaggerProperties.DocketInfo docketInfo = swaggerProperties.getDocket().get(groupName);
96105

97106
ApiInfo apiInfo = new ApiInfoBuilder()
@@ -102,33 +111,35 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
102111
.licenseUrl(docketInfo.getLicenseUrl().isEmpty() ? swaggerProperties.getLicenseUrl() : docketInfo.getLicenseUrl())
103112
.contact(
104113
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()
114+
docketInfo.getContact().getName().isEmpty() ? swaggerProperties.getContact().getName() : docketInfo.getContact().getName(),
115+
docketInfo.getContact().getUrl().isEmpty() ? swaggerProperties.getContact().getUrl() : docketInfo.getContact().getUrl(),
116+
docketInfo.getContact().getEmail().isEmpty() ? swaggerProperties.getContact().getEmail() : docketInfo.getContact().getEmail()
108117
)
109118
)
110119
.termsOfServiceUrl(docketInfo.getTermsOfServiceUrl().isEmpty() ? swaggerProperties.getTermsOfServiceUrl() : docketInfo.getTermsOfServiceUrl())
111120
.build();
112121

113122
// base-path处理
114123
// 当没有配置任何path的时候,解析/**
115-
if(docketInfo.getBasePath().isEmpty()) {
124+
if (docketInfo.getBasePath().isEmpty()) {
116125
docketInfo.getBasePath().add("/**");
117126
}
118127
List<Predicate<String>> basePath = new ArrayList();
119-
for(String path : docketInfo.getBasePath()) {
128+
for (String path : docketInfo.getBasePath()) {
120129
basePath.add(PathSelectors.ant(path));
121130
}
122131

123132
// exclude-path处理
124133
List<Predicate<String>> excludePath = new ArrayList();
125-
for(String path : docketInfo.getExcludePath()) {
134+
for (String path : docketInfo.getExcludePath()) {
126135
excludePath.add(PathSelectors.ant(path));
127136
}
128137

129138
Docket docket = new Docket(DocumentationType.SWAGGER_2)
130139
.host(swaggerProperties.getHost())
131140
.apiInfo(apiInfo)
141+
.globalOperationParameters(assemblyGlobalOperationParameters(swaggerProperties.getGlobalOperationParameters(),
142+
docketInfo.getGlobalOperationParameters()))
132143
.groupName(groupName)
133144
.select()
134145
.apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage()))
@@ -150,4 +161,49 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) {
150161
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
151162
this.beanFactory = beanFactory;
152163
}
164+
165+
private List<Parameter> buildGlobalOperationParametersFromSwaggerProperties(
166+
List<SwaggerProperties.GlobalOperationParameter> globalOperationParameters) {
167+
List<Parameter> parameters = Lists.newArrayList();
168+
for (SwaggerProperties.GlobalOperationParameter globalOperationParameter : globalOperationParameters) {
169+
parameters.add(new ParameterBuilder()
170+
.name(globalOperationParameter.getName())
171+
.description(globalOperationParameter.getDescription())
172+
.modelRef(new ModelRef(globalOperationParameter.getModelRef()))
173+
.parameterType(globalOperationParameter.getParameterType())
174+
.required(Boolean.parseBoolean(globalOperationParameter.getRequired()))
175+
.build());
176+
}
177+
return parameters;
178+
}
179+
180+
/**
181+
* 局部参数按照name覆盖局部参数
182+
*
183+
* @param globalOperationParameters
184+
* @param docketOperationParameters
185+
* @return
186+
*/
187+
private List<Parameter> assemblyGlobalOperationParameters(
188+
List<SwaggerProperties.GlobalOperationParameter> globalOperationParameters,
189+
List<SwaggerProperties.GlobalOperationParameter> docketOperationParameters) {
190+
191+
if (docketOperationParameters == null || docketOperationParameters.isEmpty()) {
192+
return buildGlobalOperationParametersFromSwaggerProperties(globalOperationParameters);
193+
}
194+
195+
Set<String> docketNames = docketOperationParameters.stream()
196+
.map(SwaggerProperties.GlobalOperationParameter::getName)
197+
.collect(Collectors.toSet());
198+
199+
List<SwaggerProperties.GlobalOperationParameter> resultOperationParameters = Lists.newArrayList();
200+
201+
for (SwaggerProperties.GlobalOperationParameter parameter : globalOperationParameters) {
202+
if (!docketNames.contains(parameter.getName())) {
203+
resultOperationParameters.add(parameter);
204+
}
205+
}
206+
resultOperationParameters.addAll(docketOperationParameters);
207+
return buildGlobalOperationParametersFromSwaggerProperties(resultOperationParameters);
208+
}
153209
}

src/main/java/com/didispace/swagger/SwaggerProperties.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import lombok.Data;
44
import lombok.NoArgsConstructor;
55
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import springfox.documentation.schema.ModelRef;
67

78
import java.util.ArrayList;
89
import java.util.LinkedHashMap;
@@ -47,6 +48,30 @@ public class SwaggerProperties {
4748
/**host信息**/
4849
private String host = "";
4950

51+
/**全局参数配置**/
52+
private List<GlobalOperationParameter> globalOperationParameters;
53+
54+
55+
@Data
56+
@NoArgsConstructor
57+
public static class GlobalOperationParameter{
58+
/**参数名**/
59+
private String name;
60+
61+
/**描述信息**/
62+
private String description;
63+
64+
/**指定参数类型**/
65+
private String modelRef;
66+
67+
/**参数放在哪个地方:header,query,path,body.form**/
68+
private String parameterType;
69+
70+
/**参数是否必须传**/
71+
private String required;
72+
73+
}
74+
5075
@Data
5176
@NoArgsConstructor
5277
public static class DocketInfo {
@@ -74,6 +99,8 @@ public static class DocketInfo {
7499
/**在basePath基础上需要排除的url规则**/
75100
private List<String> excludePath = new ArrayList<>();
76101

102+
private List<GlobalOperationParameter> globalOperationParameters;
103+
77104
}
78105

79106
@Data

0 commit comments

Comments
 (0)