引入Swagger的多种方案
第一种方案:
一、pom文件添加swagger依赖
<!-- swagger依赖 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
二、yml文件添加配置
# swagger配置
swagger:
enabled: true
title: 名称
description: "名称"
version: 1.0
base-package: com.example.managementsystem #包名
三、启动类添加注解
@EnableSwagger2Doc
第二种方案后续补充
一、引入依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
二、配置配置类(以下配置为分组模式,默认模式删除所有@bean即可)
/**
* Swagger2配置信息
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
.paths(Predicates.and(PathSelectors.regex("/XXX/.*")))
.build();
}
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/XXX/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("XXX", "http://XXX.com", "XXX"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("XXX", "http://XXX.com", "XXX"))
.build();
}
}
三、启动类添加注解
@ComponentScan(basePackages = "com.lingtong")
四、启动项目后访问
localhost:8080/swagger-ui.html#/
默认模式可以访问http://localhost:8080/v2/api-docs将得到的json串,地址可以直接导入postman