123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.care.common.config;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.env.Environment;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.service.Contact;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- import java.util.ArrayList;
- /**
- * @author javadoc
- * @version 1.0
- * @description 开启 Swagger2 注解,启动项目后在页面输入 IP:端口号/swagger-ui.html 即可进入接口文档页面
- * @date 2021/1/10 14:20
- */
- @EnableSwagger2
- @Configuration
- @Slf4j
- public class SwaggerConfig {
- /**
- * 其中一个模块接口分组配置,如果需要配置多个则多创建几个@Bean即可;
- * environment 参数用来判定当前环境,若为生产环境不启动 Swagger
- * @return
- */
- @Bean
- public Docket docketA(Environment environment){
- //获取项目的环境:
- //首先Docket 类添加 Environment 类型参数,属于 springframework 包中的类
- //设置要显示的 Swagger 环境(dev 模拟生产环境, test 是为了整明参数可以多个)
- // Profiles profiles = Profiles.of("dev", "devtest");
- //通过 environment.acceptsProfiles 判定是否处于在自己设定的环境当中
- //如果当前的生产环境属于 profiles 参数中的任意一个返回 true
- // boolean flag = environment.acceptsProfiles(profiles);
- // if(flag){
- // log.info("环境判断结果为非生产环境");
- // }
- //返回 Swagger 配置
- return new Docket(DocumentationType.SWAGGER_2)
- //设置分组名
- .groupName("接口文档A")
- //是否开启Swagger,将生产环境判定结果传到 enable() 方法,作用:生产环境不启动Swagger
- // .enable(flag)
- //Swagger API文档标题
- //Swagger API文档描述
- //版本信息
- //服务组 Url
- //作者信息
- //开源版本号
- //开源版本 Url
- .apiInfo(new ApiInfo(
- "Swagger API文档标题",
- "Swagger API文档描述",
- "V1.0",
- "106",
- new Contact("作者名", "作者博客Url", "作者邮箱Email"),
- "Apache 2.0",
- "http://www.apache.org/licenses/LICENSE-2.0",
- new ArrayList<>()
- ))
- //配置 Swagger 信息
- .select()
- //basePackage("相对包路径"):指定要扫描的包(常用)
- .apis(RequestHandlerSelectors.basePackage("com.care"))
- .build();
- }
- /**
- * 如果需要其他组可以编辑下面这个方法
- * @return
- */
- @Bean
- public Docket docketB(){
- return new Docket(DocumentationType.SWAGGER_2)
- //设置分组名
- .groupName("后台接口文档")
- .apiInfo(new ApiInfo(
- "Swagger API文档标题",
- "Swagger API文档描述",
- "V1.0",
- "106",
- new Contact("作者名", "作者博客Url", "作者邮箱Email"),
- "Apache 2.0",
- "http://www.apache.org/licenses/LICENSE-2.0",
- new ArrayList<>()
- ))
- //配置 Swagger 信息
- .select()
- //basePackage("相对包路径"):指定要扫描的包(常用)
- .apis(RequestHandlerSelectors.basePackage("com.care.bms"))
- .build();
- }
- }
|