Swagger3Config.kt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * :*$@@%$*: ;: ;; ;;
  5. * :@@%! :!@@%: %! ;%%@@%$ =@@@@@@@%; @%@@@%%%%@@@@@
  6. * :@%; :$= %%$$$%$$ ;$$ ;$@= !@$
  7. * =@! %! @ $=;% !@@@%: !$$$$$$$$$$$$$$=
  8. * =@* %! @ $= % %@= =%@! %=
  9. * *$%%! @@= ;=$%%%$*: %! @ $= % =%%%%%%@$ *%: =%
  10. * %@@!: !@@@%=$@@@@%! :*@@$: %! @ $= % $* ;@ @* :%*
  11. * ;@@! ;!!!;: ;@%: =======@%========* @ $$ % $%*****$@ :@$=*********=@$
  12. * $@* ;@@@%=!: *@*
  13. * =@$ ;;;!=%@@@@=! =@!
  14. * %@$: =@%: :*@@@* %@= Copyright (c) 2016-2019. 北京上格云技术有限公司
  15. * ;%@@$=$@@%* *@@@$=%@@%;
  16. * ::;:: ::;:: All rights reserved.
  17. *
  18. * ********************************************************************************************************************
  19. */
  20. package com.persagy
  21. import io.swagger.v3.oas.annotations.enums.ParameterIn
  22. import io.swagger.v3.oas.models.Components
  23. import io.swagger.v3.oas.models.OpenAPI
  24. import io.swagger.v3.oas.models.Operation
  25. import io.swagger.v3.oas.models.info.Contact
  26. import io.swagger.v3.oas.models.info.Info
  27. import io.swagger.v3.oas.models.info.License
  28. import io.swagger.v3.oas.models.media.StringSchema
  29. import io.swagger.v3.oas.models.parameters.Parameter
  30. import io.swagger.v3.oas.models.security.SecurityScheme
  31. import org.springdoc.core.GroupedOpenApi
  32. import org.springdoc.core.customizers.OperationCustomizer
  33. import org.springframework.beans.factory.annotation.Autowired
  34. import org.springframework.context.ApplicationContext
  35. import org.springframework.context.annotation.Bean
  36. import org.springframework.context.annotation.Configuration
  37. import org.springframework.web.method.HandlerMethod
  38. /**
  39. * RESTful API文档生成器Swagger2配置
  40. *
  41. * @author PLX
  42. */
  43. @Configuration
  44. open class Swagger3Config {
  45. /** 注入的 ApplicationContext */
  46. @Autowired
  47. lateinit var applicationContext: ApplicationContext
  48. /** 标题 */
  49. private val title = "美库(图例库) API"
  50. /** API 版本号 */
  51. private val version = "1.0"
  52. val operationCustomizer = GlobalHeaderOperationCustomizer()
  53. /**
  54. * API 信息
  55. */
  56. @Bean
  57. open fun openApi(): OpenAPI {
  58. val contact = Contact().name("张维新").email("zhangweixin@sagacloud.cn")
  59. return OpenAPI()
  60. .components(
  61. Components()
  62. .addSecuritySchemes("basicScheme", SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
  63. .info(
  64. Info()
  65. .title(title)
  66. .version(version)
  67. .termsOfService("http://www.persagy.com")
  68. .contact(contact)
  69. .license(
  70. License()
  71. .name("Apache 2.0")
  72. .url("http://www.persagy.com")))
  73. }
  74. @Bean
  75. open fun publicApi(): GroupedOpenApi? {
  76. return GroupedOpenApi.builder()
  77. .group("默认")
  78. .pathsToMatch("/**")
  79. .addOperationCustomizer(operationCustomizer)
  80. .build()
  81. }
  82. @Bean
  83. open fun umlApi(): GroupedOpenApi? {
  84. return GroupedOpenApi.builder()
  85. .group("uml")
  86. .pathsToMatch("/uml/**")
  87. .build()
  88. }
  89. // /**
  90. // * 字典 API 组
  91. // */
  92. // @Bean
  93. // open fun dictApiGroup(): GroupedOpenApi {
  94. // return GroupedOpenApi.builder().group("asf").pathsToMatch("")
  95. // .build()
  96. //
  97. // }
  98. // /**
  99. // * 对象 API 组
  100. // */
  101. // @Bean
  102. // open fun objectApiGroup(): GroupedOpenApi {
  103. // return GroupedOpenApi.builder()
  104. // .group("对象")
  105. // .pathsToMatch("/object/**")
  106. // .addOperationCustomizer(operationCustomizer)
  107. // .build()
  108. // }
  109. } // Class Swagger2Config
  110. /**
  111. * 全局参数
  112. *
  113. * @author 庞利祥 <sybotan@126.com>
  114. */
  115. class GlobalHeaderOperationCustomizer : OperationCustomizer {
  116. /**
  117. * 接口定义器
  118. *
  119. * @param operation 接口
  120. * @param handlerMethod 原接口方法
  121. * @return 新定义的接口
  122. */
  123. override fun customize(operation: Operation, handlerMethod: HandlerMethod): Operation {
  124. val projectId = Parameter().`in`(ParameterIn.HEADER.toString())
  125. .name("projectId").description("项目 id")
  126. .schema(StringSchema()).required(false)
  127. operation.addParametersItem(projectId)
  128. return operation
  129. }
  130. }