SwaggerConfig.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package com.primeton.dgs.kernel.core.configure;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import springfox.documentation.builders.ApiInfoBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import java.util.Optional;
  15. /**
  16. * <pre>
  17. *
  18. * Created by zhaopx.
  19. * User: zhaopx
  20. * Date: 2020/8/27
  21. * Time: 15:27
  22. *
  23. * </pre>
  24. *
  25. * @author zhaopx
  26. */
  27. @Configuration //必须存在
  28. @EnableSwagger2 //必须存在
  29. @ConditionalOnProperty(name = "bftconsole.swagger.enable", havingValue = "true", matchIfMissing = true)
  30. public class SwaggerConfig {
  31. @Value("${swagger.group}")
  32. private String groupName;
  33. @Value("${swagger.title}")
  34. private String title;
  35. @Value("${swagger.description}")
  36. private String description;
  37. @Value("${swagger.creator}")
  38. private String creator;
  39. @Value("${swagger.version}")
  40. private String version;
  41. @Value("${swagger.basepackage}")
  42. private String basepackage;
  43. @Bean
  44. public Docket docket() {
  45. return new Docket(DocumentationType.SWAGGER_2).groupName(groupName).apiInfo(apiInfo()).select()
  46. // 必须扫描实现类所在的包
  47. .apis(RequestHandlerSelectors.basePackage(basepackage)).paths(PathSelectors.any()).build();
  48. }
  49. // 构建api文档的详细信息函数
  50. private ApiInfo apiInfo() {
  51. return new ApiInfoBuilder()
  52. // 页面标题
  53. .title(title)
  54. // 创建人
  55. .contact(new Contact(creator, "http://www.primeton.com/" + groupName, "" ))
  56. // 版本号
  57. .version(version)
  58. // 描述
  59. .description(description).build();
  60. }
  61. }