swagger应用
一、swagger简介
官网 :https://swagger.io
在项目中使用Swagger 需要Springfox
-
Swaggwer
-
ui
Spring集成Swagger
-
新建一个Springboot项目
-
导入相关依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
-
编写一个hello工程
-
配置Swagger------写一个Config
-
测试运行
二、配置Swagger
Swagger的bean实例 ------Docket
//配置Swagger的信息---apiInfo
private ApiInfo apiInfo(){
//著者信息
Contact contact = new Contact("马志斌", "https://www.cnblogs.com/hzw6118/", "42334962@qq.com");
return new ApiInfo(
"联安API文档",
"好好学习,天天向上",
"v2.0",
"https://www.cnblogs.com/hzw6118/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
##
三、Swagger配置扫描接口
Docket.select()
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//指定要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.lianan.swagger.controller"))
//过滤什么路径
.paths(PathSelectors.ant("com.lianan.swagger.controller"))
.build(); //此处的参数是ApiInfo类的对象
}
四、配置是否启动Swagger
public class SwaggerConfig {
//配置Swagger的Docket的bean实例
五、配置API文档的分组
.groupName("联安")
六、如何配置多个分组
-
多个Docket即可
//配置Swagger的Docket的bean实例
@Bean
public Docket docket1(Environment environment) {
//设置要显示的项目的环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(profiles)监听到是否是处在自己设置的环境中
boolean falg = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("马志斌")
.apiInfo(apiInfo1())
.enable(falg);
}
@Bean
public Docket docket2(Environment environment) {
//设置要显示的项目的环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(profiles)监听到是否是处在自己设置的环境中
boolean falg = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("侯金良")
.apiInfo(apiInfo2())
.enable(falg);
}
@Bean
public Docket docket3(Environment environment) {
//设置要显示的项目的环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(profiles)监听到是否是处在自己设置的环境中
boolean falg = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("陈斌")
.apiInfo(apiInfo3())
.enable(falg);
}
@Bean
public Docket docket(Environment environment) {
//设置要显示的项目的环境
Profiles profiles = Profiles.of("dev");
//通过environment.acceptsProfiles(profiles)监听到是否是处在自己设置的环境中
boolean falg = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("联安")
//enable是否启动Swagger,如果为false,则不能启动Swagger,不能在浏览器中访问
.enable(falg)
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//指定要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.lianan.swagger.controller"))
//过滤什么路径
// .paths(PathSelectors.ant("com.lianan.swagger.controller"))
.build(); //此处的参数是ApiInfo类的对象
}
七、实体类配置
八、Controller类
九、总结
1. 我们可以通过Swagger给一些难理解的属性或接口增加一些注释信息
-
接口文档实时更新
-
可以在线测试
Swagger是一个优秀的工具,几乎所有大公司都在使用
注意:为节省运行内存,更主要为了安全起见,在发布前,一定要关闭Swagger
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)