SwaggerConfig.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.care.common.config;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.env.Environment;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. import java.util.ArrayList;
  13. /**
  14. * @author javadoc
  15. * @version 1.0
  16. * @description 开启 Swagger2 注解,启动项目后在页面输入 IP:端口号/swagger-ui.html 即可进入接口文档页面
  17. * @date 2021/1/10 14:20
  18. */
  19. @EnableSwagger2
  20. @Configuration
  21. @Slf4j
  22. public class SwaggerConfig {
  23. /**
  24. * 其中一个模块接口分组配置,如果需要配置多个则多创建几个@Bean即可;
  25. * environment 参数用来判定当前环境,若为生产环境不启动 Swagger
  26. * @return
  27. */
  28. @Bean
  29. public Docket docketA(Environment environment){
  30. //获取项目的环境:
  31. //首先Docket 类添加 Environment 类型参数,属于 springframework 包中的类
  32. //设置要显示的 Swagger 环境(dev 模拟生产环境, test 是为了整明参数可以多个)
  33. // Profiles profiles = Profiles.of("dev", "devtest");
  34. //通过 environment.acceptsProfiles 判定是否处于在自己设定的环境当中
  35. //如果当前的生产环境属于 profiles 参数中的任意一个返回 true
  36. // boolean flag = environment.acceptsProfiles(profiles);
  37. // if(flag){
  38. // log.info("环境判断结果为非生产环境");
  39. // }
  40. //返回 Swagger 配置
  41. return new Docket(DocumentationType.SWAGGER_2)
  42. //设置分组名
  43. .groupName("接口文档A")
  44. //是否开启Swagger,将生产环境判定结果传到 enable() 方法,作用:生产环境不启动Swagger
  45. // .enable(flag)
  46. //Swagger API文档标题
  47. //Swagger API文档描述
  48. //版本信息
  49. //服务组 Url
  50. //作者信息
  51. //开源版本号
  52. //开源版本 Url
  53. .apiInfo(new ApiInfo(
  54. "Swagger API文档标题",
  55. "Swagger API文档描述",
  56. "V1.0",
  57. "106",
  58. new Contact("作者名", "作者博客Url", "作者邮箱Email"),
  59. "Apache 2.0",
  60. "http://www.apache.org/licenses/LICENSE-2.0",
  61. new ArrayList<>()
  62. ))
  63. //配置 Swagger 信息
  64. .select()
  65. //basePackage("相对包路径"):指定要扫描的包(常用)
  66. .apis(RequestHandlerSelectors.basePackage("com.care"))
  67. .build();
  68. }
  69. /**
  70. * 如果需要其他组可以编辑下面这个方法
  71. * @return
  72. */
  73. @Bean
  74. public Docket docketB(){
  75. return new Docket(DocumentationType.SWAGGER_2)
  76. //设置分组名
  77. .groupName("后台接口文档")
  78. .apiInfo(new ApiInfo(
  79. "Swagger API文档标题",
  80. "Swagger API文档描述",
  81. "V1.0",
  82. "106",
  83. new Contact("作者名", "作者博客Url", "作者邮箱Email"),
  84. "Apache 2.0",
  85. "http://www.apache.org/licenses/LICENSE-2.0",
  86. new ArrayList<>()
  87. ))
  88. //配置 Swagger 信息
  89. .select()
  90. //basePackage("相对包路径"):指定要扫描的包(常用)
  91. .apis(RequestHandlerSelectors.basePackage("com.care.bms"))
  92. .build();
  93. }
  94. }