Spring Boot 3项目集成Swagger3教程

Spring Boot 3项目集成Swagger3教程

Swagger是一个用于设计、构建、记录和使用RESTful web服务的开源软件框架。Swagger 3(OpenAPI 3.0)提供了更加强大和灵活的API文档生成能力。本教程将指导您如何在Spring Boot 3项目中集成Swagger3,并使用Knife4j作为UI界面。

1. 添加依赖

首先,您需要在项目的pom.xml文件中添加Swagger3的依赖。同时,为了确保依赖能够正确下载,您可以添加阿里云的Maven镜像仓库。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <!--swagger3-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>


<repositories>
<!--阿里云镜像-->
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

2. 配置Swagger

在Spring Boot项目中创建一个配置类SwaggerConfig,并添加Swagger的配置信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("标题")
.contact(new Contact())
.description("我的API文档")
.version("v1")
.license(new License().name("Apache 2.0").url("http://springdoc.org")))
.externalDocs(new ExternalDocumentation()
.description("外部文档")
.url("https://springshop.wiki.github.org/docs"));
}
}


3. 实体类和控制层注解

在您的实体类和控制层中使用Swagger注解来描述API。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 实体类注解示例
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Date;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(name = "Employee", description = "$!{table.comment}")
public class Emp {
@ExcelProperty("ID")
@Schema(description = "ID")
private int id;
@ExcelProperty("用户名")
@Schema(description = "用户名")
private String names;
@ExcelProperty("工资")
@Schema(description = "工资")
private double salary;
@ExcelProperty("生日")
@Schema(description = "生日")
private Date birthday;
@ColumnWidth(20)
@ExcelProperty("头像")
@Schema(description = "头像")
private String photo;

// @ColumnWidth(20)
// @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
// @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
// @ExcelProperty("创建日期")
// private Date u_create_time;
}


// 控制层注解示例
import io.swagger.v3.oas.annotations.Operation;

@Operation(summary = "获取所有员工信息")
@GetMapping("/selectAll")
public List<Emp> selectAll() {
// ...
}

4. 通用返回结果封装

创建一个通用的返回结果类,用于统一API的响应格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Builder(toBuilder = true)
@AllArgsConstructor
@Setter
@Getter
@Slf4j
public class Result<T> {
/**
* 提示信息
*/
@Schema(description = "提示信息")
private String message;
/**
* 是否成功
*/
@Schema(description = "是否成功")
private boolean success;
/**
* 返回状态码
*/
@Schema(description = "返回状态码")
private Integer code;
/**
* 数据
*/
@Schema(description = "数据")
private T data;

public Result() {
}

public static Result success() {
Result Result = new Result();
Result.setSuccess(Boolean.TRUE);
Result.setCode(ResultCode.SUCCESS.getCode());
Result.setMessage(ResultCode.SUCCESS.getMsg());
return Result;
}

public static Result success(String msg) {
Result Result = new Result();
Result.setMessage(msg);
Result.setSuccess(Boolean.TRUE);
Result.setCode(ResultCode.SUCCESS.getCode());
return Result;
}

public static Result success(Object data) {
Result Result = new Result();
Result.setData(data);
Result.setSuccess(Boolean.TRUE);
Result.setCode(ResultCode.SUCCESS.getCode());
Result.setMessage(ResultCode.SUCCESS.getMsg());
return Result;
}

/**
* 返回失败 消息
*
* @return Result
*/
public static Result failure() {
Result Result = new Result();
Result.setSuccess(Boolean.FALSE);
Result.setCode(ResultCode.FAILURE.getCode());
Result.setMessage(ResultCode.FAILURE.getMsg());
return Result;
}

/**
* 返回失败 消息
*
* @param msg 失败信息
* @return Result
*/
public static Result failure(String msg) {
Result Result = new Result();
Result.setSuccess(Boolean.FALSE);
Result.setCode(ResultCode.FAILURE.getCode());
Result.setMessage(msg);
return Result;
}

public static Result failure(Integer code, String msg) {
Result Result = new Result();
Result.setSuccess(Boolean.FALSE);
Result.setCode(code);
Result.setMessage(msg);
return Result;
}


public static Result failure(String msg, ResultCode exceptionCode) {
Result Result = new Result();
Result.setMessage(msg);
Result.setSuccess(Boolean.FALSE);
Result.setCode(exceptionCode.getCode());
Result.setData(exceptionCode.getMsg());
return Result;
}

/**
* 返回失败 消息
*
* @param exceptionCode 错误信息枚举
* @return Result
*/
public static Result failure(ResultCode exceptionCode) {
Result Result = new Result();
Result.setSuccess(Boolean.FALSE);
Result.setCode(exceptionCode.getCode());
Result.setMessage(exceptionCode.getMsg());
return Result;
}

/**
* 返回失败 消息
*
* @param exceptionCode 错误信息枚举
* @param msg 自定义错误提示信息
* @return Result
*/
public static Result failure(ResultCode exceptionCode, String msg) {
Result Result = new Result();
Result.setMessage(msg);
Result.setSuccess(Boolean.FALSE);
Result.setCode(exceptionCode.getCode());
return Result;
}

}

5. 注解说明

Swagger3的注解与Swagger2有所不同,以下是一些常用注解的对照表:

Swagger2注解 Swagger3注解 注解位置
@Api @Tag(name = “接口类描述”) Controller类上
@ApiOperation @Operation(summary = “接口方法描述”) Controller方法上
@ApiImplicitParams @Parameters Controller方法上
@ApiImplicitParam @Parameter(description = “参数描述”) Controller方法上
@ApiParam @Parameter(description = “参数描述”) 方法参数上
@ApiIgnore @Parameter(hidden = true) 或 @Operation(hidden = true) -
@ApiModel @Schema DTO类上
@ApiModelProperty @Schema DTO属性上

6. 访问Swagger UI

启动您的Spring Boot应用后,您可以通过以下地址访问Swagger UI:

1
2
http://localhost:8080/doc.html
http://localhost:8080/swagger-ui/index.html

在这里,您可以查看API文档,测试API接口,并获取相关信息。

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 何福海
  • 访问人数: | 浏览次数:

请我喝杯奶茶吧~

支付宝
微信