本项目是在Swagger3.0版本下的使用
-
导入相关依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
-
定义一个Swagger配置类,在类上加上@Configuration和@EnableSwagger2注解
@Configuration @EnableSwagger2 //开启Swagger2 public class SwaggerConfig { //注册多个Docket分组实例,并设置右上角显示的分组名称 @Bean public Docket getDocket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("A"); } //注册Docket的bean实例对象 @Bean public Docket getDocket(Environment environment){ //配置要显示Swagger的环境 Profiles profiles = Profiles.of("dev"); //通过environment.acceptsProfiles()判断是否处于自己设定的环境中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //右上角显示的分组名称 .groupName("***") .select() //RequestHandlerSelectors 配置要扫描接口的方式 //any() 扫描所有接口 Default //basePackage() 扫描指定的包 //none() 全都不扫描 //withClassAnnotation() 扫描类上指定的注解 //withMethodAnnotation() 扫描指定方法上的注解 .apis(RequestHandlerSelectors.any()) //PathSelectors 配置需要过滤(扫描)的路径 //ant() 过滤(扫描)指定的路径 //any() 过滤(扫描)所有的路径 Default //none() 全都不过滤(扫描) //regex() 正则表达式的路径 .paths(PathSelectors.any()) .build() //enable() 是否启用Swagger,若为false,则不能访问Swagger .enable(flag); } //配置Swagger2的apiInfo信息 private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact("***", "https://www.****.com/", "*******@qq.com"); return new ApiInfo("、、、的API文档", //title Api文档标题 "就是一点点小描述咯", //description 简单描述 "1.0", //version 版本号 "https://www.*****.com/", contact, //作者信息描述 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
-
在启动类上加@EnableOpenApi注解
-
测试url路径
http://localhost:8081/swagger-ui/index.html