SwaggerConfig.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.yihu.jw;
  2. import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.ParameterBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.schema.ModelRef;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.service.Contact;
  12. import springfox.documentation.service.Parameter;
  13. import springfox.documentation.spi.DocumentationType;
  14. import springfox.documentation.spring.web.plugins.Docket;
  15. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import static com.google.common.base.Predicates.or;
  19. import static springfox.documentation.builders.PathSelectors.regex;
  20. //@EnableKnife4j
  21. @Configuration
  22. @EnableSwagger2
  23. @EnableSwaggerBootstrapUI
  24. public class SwaggerConfig {
  25. public static final String API_VERSION = "v1.0";
  26. final String userAgentJson = "{\"id\":int,\"uid\":string,\"openid\":string,\"token\":string,\"lastUid\":string,\"platform\":int}";
  27. private List<Parameter> addUseragent() {
  28. ParameterBuilder tokenPar = new ParameterBuilder();
  29. List<Parameter> pars = new ArrayList<>();
  30. tokenPar.name("userAgent").description(userAgentJson).modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("").build();
  31. pars.add(tokenPar.build());
  32. return pars;
  33. }
  34. @Bean
  35. public Docket publicAPI() {
  36. List<Parameter> pars = addUseragent();
  37. return new Docket(DocumentationType.SWAGGER_2)
  38. .groupName("Default")
  39. .useDefaultResponseMessages(false)
  40. .forCodeGeneration(true)
  41. .pathMapping("/")
  42. .select()
  43. .apis(RequestHandlerSelectors.basePackage("com.yihu"))
  44. .paths(PathSelectors.any())
  45. .build()
  46. .globalOperationParameters(pars)
  47. .apiInfo(publicApiInfo());
  48. }
  49. @Bean
  50. public Docket privateAPI() {
  51. return new Docket(DocumentationType.SWAGGER_2)
  52. .groupName("Private")
  53. .useDefaultResponseMessages(false)
  54. .forCodeGeneration(false)
  55. .pathMapping("/")
  56. .select()
  57. .paths(or(regex("/archaius"),
  58. regex("/loggers*"),
  59. regex("/mappings"),
  60. regex("/pause"),
  61. regex("/beans"),
  62. regex("/health"),
  63. regex("/resume"),
  64. regex("/heapdump"),
  65. regex("/env*"),
  66. regex("/shutdown"),
  67. regex("/configprops"),
  68. regex("/auditevents"),
  69. regex("/restart"),
  70. regex("/trace"),
  71. regex("/metrics*"),
  72. regex("/refresh"),
  73. regex("/dump"),
  74. regex("/autoconfig"),
  75. regex("/info"),
  76. regex("/ChildManageController"),
  77. regex("/features")))
  78. .build()
  79. .apiInfo(privateAPIInfo());
  80. }
  81. @Bean
  82. public Docket legacyAPI(){
  83. return new Docket(DocumentationType.SWAGGER_2)
  84. .groupName("Legacy")
  85. .useDefaultResponseMessages(false)
  86. .forCodeGeneration(false)
  87. .pathMapping("/")
  88. .select()
  89. .paths(or(regex("/legacy/rest/v.*")))
  90. .build()
  91. .apiInfo(legacyApiInfo());
  92. }
  93. /**
  94. * 测试client信息。
  95. *
  96. * @return
  97. */
  98. /*@Bean
  99. SecurityConfiguration security() {
  100. return new SecurityConfiguration(
  101. "ehr-browser",
  102. "secret123",
  103. "ROLE_CLIENT",
  104. "test-app",
  105. "ac04-47ec-9a9a-7c47bbcbbbd1");
  106. }*/
  107. private ApiInfo publicApiInfo() {
  108. return new ApiInfoBuilder()
  109. .title("众健信联平台API")
  110. .description("众健信联平台API,提供后端基础数据接口")
  111. .version(API_VERSION)
  112. .termsOfServiceUrl("https://www.yihu.com")
  113. .contact(new Contact("Jkzl Xiamen R & D Center Platform Development.", "https://www.yihu.com", "jzkl@jkzl.com"))
  114. .license("The Apache License, Version 2.0")
  115. .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
  116. .build();
  117. }
  118. private ApiInfo privateAPIInfo() {
  119. return new ApiInfoBuilder()
  120. .title("众健信联平台API")
  121. .description("众健信联平台API,提供微服务监控接口")
  122. .version(API_VERSION)
  123. .termsOfServiceUrl("https://www.yihu.com")
  124. .contact(new Contact("Jkzl Xiamen R & D Center Platform Development.", "https://www.yihu.com", "jzkl@jkzl.com"))
  125. .license("The Apache License, Version 2.0")
  126. .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
  127. .build();
  128. }
  129. private ApiInfo legacyApiInfo() {
  130. return new ApiInfoBuilder()
  131. .title("众健信联平台API")
  132. .description("众健信联平台API,提供遗留版本后端基础数据接口")
  133. .version(API_VERSION)
  134. .termsOfServiceUrl("https://www.yihu.com")
  135. .contact(new Contact("Jkzl Xiamen R & D Center Platform Development.", "https://www.yihu.com", "jzkl@jkzl.com"))
  136. .license("The Apache License, Version 2.0")
  137. .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
  138. .build();
  139. }
  140. }