1 package cn.home1.oss.lib.swagger.starter;
2
3 import static cn.home1.oss.lib.swagger.SwaggerUtils.apiInfo;
4 import static com.google.common.base.Predicates.or;
5
6 import com.google.common.base.Predicate;
7
8 import cn.home1.oss.boot.autoconfigure.PathUtils;
9
10 import com.fasterxml.classmate.ResolvedType;
11 import com.fasterxml.classmate.TypeResolver;
12
13 import lombok.extern.slf4j.Slf4j;
14
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
17 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
18 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
19 import org.springframework.context.annotation.Bean;
20 import org.springframework.context.annotation.Configuration;
21
22 import springfox.documentation.builders.RequestHandlerSelectors;
23 import springfox.documentation.spi.DocumentationType;
24 import springfox.documentation.spring.web.plugins.Docket;
25
26 import java.util.Optional;
27
28
29
30
31 @ConditionalOnClass({ManagementServerProperties.class})
32 @Configuration
33 @Slf4j
34 public class ManagementConfiguration {
35
36 public static final String DOCKET_MANAGEMENT = "docketManagement";
37
38 @Autowired(required = false)
39 private ManagementServerProperties managementServerProperties;
40
41 @ConditionalOnMissingBean(name = DOCKET_MANAGEMENT)
42 @Bean(name = DOCKET_MANAGEMENT)
43 public Docket docketManagement() {
44 final Docket docket = new Docket(DocumentationType.SWAGGER_2)
45 .apiInfo(apiInfo("spring-boot-starter-actuator", "spring boot's management endpoints"))
46 .groupName("management endpoints")
47 .select()
48 .apis(or(RequestHandlerSelectors.basePackage("org.springframework.boot.actuate"),
49 RequestHandlerSelectors.basePackage("org.springframework.cloud.context")))
50 .paths(this.managementPaths())
51 .build();
52
53 final Optional<ResolvedType> modelResolvedError = modelResolvedError();
54 return modelResolvedError.isPresent() ? docket.additionalModels(modelResolvedError.get()) : docket;
55 }
56
57 @Bean(name = NoManagementConfiguration.MANAGEMENT_PATHS)
58 public Predicate<String> managementPaths() {
59
60
61 final String managementContextPath = this.managementServerProperties != null ?
62 this.managementServerProperties.getContextPath() : "";
63 return input -> PathUtils.managementPaths(managementContextPath).test(input);
64 }
65
66 static Optional<ResolvedType> modelResolvedError() {
67 Optional<ResolvedType> result;
68 try {
69 result = Optional.ofNullable(new TypeResolver().resolve(
70 Class.forName("cn.home1.oss.lib.errorhandle.api.ResolvedError")));
71 } catch (final ClassNotFoundException ex) {
72 log.trace("Swagger can't find class cn.home1.oss.lib.errorhandle.api.ResolvedError," +
73 "so api will not show this info in swagger api document.", ex);
74 result = Optional.empty();
75 }
76 return result;
77 }
78 }