-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathDocketConfiguration.java
More file actions
224 lines (196 loc) · 9.37 KB
/
DocketConfiguration.java
File metadata and controls
224 lines (196 loc) · 9.37 KB
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
package com.spring4all.swagger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 com.google.common.base.Predicates;
import springfox.documentation.RequestHandler;
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.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.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;
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).select()
.apis(apis(swaggerProperties.getBasePackages()))
.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(host()).apiInfo(apiInfo).select()
.apis(apis(docketInfo.getBasePackages()))
.paths(paths(docketInfo.getBasePath(), docketInfo.getExcludePath())).build();
}
}
private String host() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return "127.0.0.1";
}
}
/**
* 同组多包配置
* @param basePackages
* @return
*/
private Predicate apis(List<String> basePackages) {
Predicate<RequestHandler> predicate = null;
for (String basePackage : basePackages) {
Predicate<RequestHandler> basePackagePredicate = RequestHandlerSelectors.basePackage(basePackage);
if (predicate == null) {
predicate = basePackagePredicate;
} else {
predicate.or(basePackagePredicate);
}
}
return predicate;
}
/**
* 全局请求参数
*
* @param swaggerProperties
* {@link SwaggerProperties}
* @return RequestParameter {@link RequestParameter}
*/
private List<RequestParameter> globalRequestParameters(SwaggerProperties swaggerProperties) {
return swaggerProperties.getGlobalOperationParameters().stream()
.map(param -> new RequestParameterBuilder().name(param.getName()).description(param.getDescription())
.in(param.getParameterType()).required(param.getRequired())
.query(q -> q.defaultValue(param.getModelRef()))
.query(q -> q.model(m -> m.scalarModel(ScalarType.STRING))).build())
.collect(Collectors.toList());
}
/**
* 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();
}
}