「这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战」
1. 配置Swagger
1.1 配置Swagger-ui页面的基本信息
由于没有set方法,所以只能使用构造器进行设置
不过一般需要设置的的地方很少,都是固定写法就可以了
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo();
}
//配置Swagger信息==>ApiInfo
public ApiInfo apiInfo(){
//作者信息
Contact contact=new Contact("杨明宇","http://blog.ymy12138.cn","1064576969@qq.com");
return new ApiInfo("Api Documentation",//标题
"Api Documentation",//详细描述
"1.0",//版本
"urn:tos",//开发者信息,可以是个网站
contact,//作者信息
"Apache 2.0",//依赖
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
复制代码
1.2 配置Swagger扫描接口
也就是配置扫描Controller和对应的返回值
使用链式的写法在创建Docket的时候传入其中作为参数
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.enable(true);
.apis(RequestHandlerSelectors.xxx())
.paths(PathSelectors.yyy());
.build();
//apis,扫描哪些
//xxx
//1. basePackage:指定要扫描的包,一般就使用这个就可以了
//2. any():扫描全部
//3. none():不扫描
//4. withClassAnnotation:扫描类上的注解,注解的反射对象
//5. withMethodAnnotation:扫描方法上的注解
//****************************************************
//paths,过滤掉哪些
//yyy
//1. ant():表示过滤哪个包
//2. any():表示过滤全部
//3. none():表示都不过滤
//**************************************
//enable,表示是否打开swagger的功能,默认true,如果修改成false,那么就无法正常使用swagger功能
复制代码
问题
如何做到swagger在生产环境中使用,在发布的环境中不使用
- 判断是不是生产环境
- 使用springBoot自带的设置环境的方法,创建多个配置文件application.properties
- 在主配置文件中指定当前的激活的环境是哪个
spring.profiles.active=dev
,dev就是application-xxx.properties
中的xxx - 在代码中获取当前的环境,再进行判断是否是生产环境
//在创建Docket的时候让其传入一个Environment对象,注意是springframe的包下的 Profiles profiles=Profiles.of("dev","test"); boolean flag=environment.acceptsProfiles(profiles); //这样如果是dev环境,就会返回true 复制代码
- 注入enable()
return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(flag); .select() .apis(RequestHandlerSelectors.xxx()) .paths(PathSelectors.yyy()); .build(); 复制代码
这样之后,只需要在配置文件中确定当前是在哪个环境就可以改变是否会显示Swagger
2. 配置分组
在创建Docket的时候可以传入一个.groupname("组名")
来设置当前的文档是在哪个组下的
如何配置多个组?
如果有多个Docket实例就会有多个组,这样就会出现很多个组在右上角的索引中
总结
最后的config
package com.ymy.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Documentation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import sun.security.acl.AllPermissionsImpl;
import java.util.ArrayList;
/**
* @Version 1.0
* @Author:ymy
* @Date:2019/8/31
* @Content:
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment){
Profiles profiles=Profiles.of("dev","test");
boolean flag=environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.ymy"))
.build();
}
//配置Swagger信息==>ApiInfo
public ApiInfo apiInfo(){
//作者信息
Contact contact=new Contact("杨明宇","http://blog.ymy12138.cn","1064576969@qq.com");
return new ApiInfo("Api Documentation",//标题
"Api Documentation",//详细描述
"1.0",//版本
"urn:tos",//开发者信息,可以是个网站
contact,//作者信息
"Apache 2.0",//依赖
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
复制代码
这样的话,就需要设置当前的环境是在dev的环境下才可以看到swagger
如果不设置就使用下面这个
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo();
}
//配置Swagger信息==>ApiInfo
public ApiInfo apiInfo(){
//作者信息
Contact contact=new Contact("杨明宇","http://blog.ymy12138.cn","1064576969@qq.com");
return new ApiInfo("Api Documentation",//标题
"Api Documentation",//详细描述
"1.0",//版本
"urn:tos",//开发者信息,可以是个网站
contact,//作者信息
"Apache 2.0",//依赖
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
复制代码
近期评论