-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringfoxLoaderConfig.java
133 lines (115 loc) · 6.32 KB
/
SpringfoxLoaderConfig.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
package com.github.springfox.loader;
import com.github.springfox.loader.plugins.LoaderOperationPlugin;
import com.github.springfox.loader.plugins.LoaderTagProvider;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.*;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ObjectVendorExtension;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.readers.operation.DefaultTagsProvider;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@EnableConfigurationProperties
@Configuration
@ComponentScan(basePackageClasses = SpringfoxLoaderConfig.class)
public class SpringfoxLoaderConfig implements WebMvcConfigurer, ApplicationContextAware, EmbeddedValueResolverAware {
private SpringfoxLoader springfoxLoader = new SpringfoxLoader();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
springfoxLoader.setApplicationContext(applicationContext);
}
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
springfoxLoader.setStringValueResolver(stringValueResolver);
}
@Autowired
public void setSpringfoxLoaderProps(SpringfoxLoaderProps loaderProps) {
springfoxLoader.setSpringfoxLoaderProps(loaderProps);
}
@Bean
@Conditional(ActiveProfilesCondition.class)
public Docket api() {
ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).select();
Predicate<RequestHandler> predicate = RequestHandlerSelectors.basePackage(springfoxLoader.getBasePackage())::apply;
if (springfoxLoader.includeControllers().length > 0) {
Class<?>[] controllers = springfoxLoader.includeControllers();
for (Class<?> controller : controllers) {
Predicate<RequestHandler> includeControllerRequestHandler = RequestHandlerSelectors.basePackage(controller.getPackage().getName())::apply;
predicate = predicate.or(includeControllerRequestHandler);
}
}
apiSelectorBuilder.apis(predicate::test);
apiSelectorBuilder.paths(PathSelectors.any()).build().apiInfo(apiInfo()).pathMapping(springfoxLoader.getPath());
return apiSelectorBuilder.build();
}
private ApiInfo apiInfo() {
return new ApiInfo(springfoxLoader.getTitle(), springfoxLoader.getDescription(), springfoxLoader.getVersion(),
springfoxLoader.getTermsOfServiceUrl(), springfoxLoader.getContact(), springfoxLoader.getLicense(), springfoxLoader.getLicenseUrl(), getVendorExtensions());
}
private List<VendorExtension> getVendorExtensions() {
Extension[] extensions = springfoxLoader.extensions();
if (extensions.length == 1 && StringUtils.isEmpty(extensions[0].name())) {
return Collections.emptyList();
}
return Arrays.stream(extensions).map(extension -> {
ExtensionProperty[] extensionProperties = extension.properties();
List<StringVendorExtension> vendorExtensions = Arrays.stream(extensionProperties)
.map(property -> new StringVendorExtension(property.name(), property.value())).collect(Collectors.toList());
ObjectVendorExtension vendorExtension = new ObjectVendorExtension(extension.name());
vendorExtensions.forEach(vendorExtension::addProperty);
return vendorExtension;
}).collect(Collectors.toList());
}
@Bean
@Primary
@Conditional(ActiveProfilesCondition.class)
public DefaultTagsProvider loaderDefaultTagsProvider() {
return new LoaderTagProvider(springfoxLoader.conventionMode());
}
@Bean
@Conditional(ActiveProfilesCondition.class)
public LoaderOperationPlugin loaderOperationPlugin() {
return new LoaderOperationPlugin(springfoxLoader.conventionMode());
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
if (!StringUtils.isEmpty(springfoxLoader.swaggerUiBasePath())) {
registry.addRedirectViewController(resourcePath("/v2/api-docs"), "/v2/api-docs");
registry.addRedirectViewController(resourcePath("/swagger-resources/configuration/ui"), "/swagger-resources/configuration/ui");
registry.addRedirectViewController(resourcePath("/swagger-resources/configuration/security"), "/swagger-resources/configuration/security");
registry.addRedirectViewController(resourcePath("/swagger-resources"), "/swagger-resources");
}
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!StringUtils.isEmpty(springfoxLoader.swaggerUiBasePath())) {
registry.addResourceHandler(resourcePath("/swagger-ui.html**")).addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler(resourcePath("/webjars/**")).addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
private String resourcePath(String path) {
return springfoxLoader.swaggerUiBasePath() + path;
}
}