-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathDocketConfiguration.java
248 lines (214 loc) · 11 KB
/
DocketConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.spring4all.swagger;
import static com.google.common.collect.Lists.newArrayList;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import com.google.common.base.Predicates;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Docket配置
*
* @author 程序猿DD
* @author andi.lin
*
* Created on 2017/8/7 Update on 2021/8/13
*/
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class DocketConfiguration implements BeanFactoryAware {
private BeanFactory beanFactory;
@Autowired
private SwaggerProperties swaggerProperties;
@Autowired
private SwaggerAuthorizationConfiguration authConfiguration;
private static final String BEAN_NAME = "spring-boot-starter-swagger-";
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
/**
* Create the corresponding configuration for DocumentationPluginRegistry
*/
@Bean
public void createSpringFoxRestApi() {
BeanDefinitionRegistry beanRegistry = (BeanDefinitionRegistry)beanFactory;
// 没有分组
if (swaggerProperties.getDocket().size() == 0) {
String beanName = BEAN_NAME + "default";
BeanDefinition beanDefinition4Group = new GenericBeanDefinition();
beanDefinition4Group.getConstructorArgumentValues().addIndexedArgumentValue(0, DocumentationType.OAS_30);
beanDefinition4Group.setBeanClassName(Docket.class.getName());
beanDefinition4Group.setRole(BeanDefinition.ROLE_SUPPORT);
beanRegistry.registerBeanDefinition(beanName, beanDefinition4Group);
Docket docket4Group = (Docket)beanFactory.getBean(beanName);
ApiInfo apiInfo = apiInfo(swaggerProperties);
docket4Group.host(swaggerProperties.getHost()).apiInfo(apiInfo)
.globalRequestParameters(
assemblyRequestParameters(swaggerProperties.getGlobalOperationParameters(), new ArrayList<>()))
.securityContexts(Collections.singletonList(authConfiguration.securityContext()))
.securitySchemes(authConfiguration.getSecuritySchemes()).select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(paths(swaggerProperties.getBasePath(), swaggerProperties.getExcludePath())).build();
return;
}
for (Map.Entry<String, SwaggerProperties.DocketInfo> entry : swaggerProperties.getDocket().entrySet()) {
String groupName = entry.getKey();
SwaggerProperties.DocketInfo docketInfo = entry.getValue();
String beanName = BEAN_NAME + groupName;
ApiInfo apiInfo = new ApiInfoBuilder()
.title(docketInfo.getTitle().isEmpty() ? swaggerProperties.getTitle() : docketInfo.getTitle())
.description(docketInfo.getDescription().isEmpty() ? swaggerProperties.getDescription()
: docketInfo.getDescription())
.version(docketInfo.getVersion().isEmpty() ? swaggerProperties.getVersion() : docketInfo.getVersion())
.license(docketInfo.getLicense().isEmpty() ? swaggerProperties.getLicense() : docketInfo.getLicense())
.licenseUrl(docketInfo.getLicenseUrl().isEmpty() ? swaggerProperties.getLicenseUrl()
: docketInfo.getLicenseUrl())
.contact(new Contact(
docketInfo.getContact().getName().isEmpty() ? swaggerProperties.getContact().getName()
: docketInfo.getContact().getName(),
docketInfo.getContact().getUrl().isEmpty() ? swaggerProperties.getContact().getUrl()
: docketInfo.getContact().getUrl(),
docketInfo.getContact().getEmail().isEmpty() ? swaggerProperties.getContact().getEmail()
: docketInfo.getContact().getEmail()))
.termsOfServiceUrl(docketInfo.getTermsOfServiceUrl().isEmpty()
? swaggerProperties.getTermsOfServiceUrl() : docketInfo.getTermsOfServiceUrl())
.build();
// base-path处理
// 当没有配置任何path的时候,解析/**
if (docketInfo.getBasePath().isEmpty()) {
docketInfo.getBasePath().add("/**");
}
List<Predicate<String>> basePath = new ArrayList<>();
for (String path : docketInfo.getBasePath()) {
basePath.add(PathSelectors.ant(path));
}
// exclude-path处理
List<Predicate<String>> excludePath = new ArrayList<>();
for (String path : docketInfo.getExcludePath()) {
excludePath.add(PathSelectors.ant(path));
}
BeanDefinition beanDefinition4Group = new GenericBeanDefinition();
beanDefinition4Group.getConstructorArgumentValues().addIndexedArgumentValue(0, DocumentationType.OAS_30);
beanDefinition4Group.setBeanClassName(Docket.class.getName());
beanDefinition4Group.setRole(BeanDefinition.ROLE_SUPPORT);
beanRegistry.registerBeanDefinition(beanName, beanDefinition4Group);
Docket docket4Group = (Docket)beanFactory.getBean(beanName);
docket4Group.groupName(groupName).host(docketInfo.getBasePackage()).apiInfo(apiInfo)
.globalRequestParameters(assemblyRequestParameters(swaggerProperties.getGlobalOperationParameters(),
docketInfo.getGlobalOperationParameters()))
.securityContexts(Collections.singletonList(authConfiguration.securityContext()))
.securitySchemes(authConfiguration.getSecuritySchemes()).select()
.apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage()))
.paths(paths(docketInfo.getBasePath(), docketInfo.getExcludePath())).build();
}
}
/**
* 全局请求参数
*
* @param properties
* {@link SwaggerProperties}
* @return RequestParameter {@link RequestParameter}
*/
private List<RequestParameter> getRequestParameters(List<SwaggerProperties.GlobalOperationParameter> properties) {
if (CollectionUtils.isEmpty(properties)) {
return new ArrayList<>();
}
return properties.stream()
.map(param -> new RequestParameterBuilder().name(param.getName()).description(param.getDescription())
.in(ParameterType.from(param.getParameterType())).required(param.getRequired())
.query(q -> q.defaultValue(param.getType()))
.query(q -> q.model(m -> m.scalarModel(!ScalarType.from(param.getType(), param.getFormat()).isPresent()
? ScalarType.STRING : ScalarType.from(param.getType(), param.getFormat()).get())))
.build())
.collect(Collectors.toList());
}
/**
* 局部参数按照name覆盖局部参数
*
* @param globalRequestParameters
* 全局配置
* @param groupRequestParameters
* Group 的配置
* @return 汇总配置
*/
private List<RequestParameter> assemblyRequestParameters(
List<SwaggerProperties.GlobalOperationParameter> globalRequestParameters,
List<SwaggerProperties.GlobalOperationParameter> groupRequestParameters) {
if (Objects.isNull(groupRequestParameters) || groupRequestParameters.isEmpty()) {
return getRequestParameters(globalRequestParameters);
}
Set<String> paramNames = groupRequestParameters.stream()
.map(SwaggerProperties.GlobalOperationParameter::getName).collect(Collectors.toSet());
List<SwaggerProperties.GlobalOperationParameter> requestParameters = newArrayList();
if (Objects.nonNull(globalRequestParameters)) {
for (SwaggerProperties.GlobalOperationParameter parameter : globalRequestParameters) {
if (!paramNames.contains(parameter.getName())) {
requestParameters.add(parameter);
}
}
}
requestParameters.addAll(groupRequestParameters);
return getRequestParameters(requestParameters);
}
/**
* API接口路径选择
*
* @param basePath
* basePath
* @param excludePath
* excludePath
* @return path
*/
private Predicate paths(List<String> basePath, List<String> excludePath) {
// base-path处理
// 当没有配置任何path的时候,解析/**
if (basePath.isEmpty()) {
basePath.add("/**");
}
List<com.google.common.base.Predicate<String>> basePathList = new ArrayList<>();
for (String path : basePath) {
basePathList.add(PathSelectors.ant(path));
}
// exclude-path处理
List<com.google.common.base.Predicate<String>> excludePathList = new ArrayList<>();
for (String path : excludePath) {
excludePathList.add(PathSelectors.ant(path));
}
return Predicates.and(Predicates.not(Predicates.or(excludePathList)), Predicates.or(basePathList));
}
/**
* API文档基本信息
*
* @param swaggerProperties
* swaggerProperties
* @return apiInfo
*/
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription())
.version(swaggerProperties.getVersion()).license(swaggerProperties.getLicense())
.licenseUrl(swaggerProperties.getLicenseUrl())
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(),
swaggerProperties.getContact().getEmail()))
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl()).build();
}
}