距离springfox的swagger2.x 以及3.0.0 长久等待,等来了springdoc的swagger 3 为啥是3是因为支持openapi3.0规范(最新支持3.1)
maven pom.xml 添加
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
application.yml配置
#文档部分配置
springdoc:
api-docs:
enabled: true
version: openapi_3_1
groups:
enabled: true
swagger-ui:
display-request-duration: true
groups-order: desc
operations-sorter: method
disable-swagger-default-url: true
use-root-path: true
default-model-expand-depth: 5
# default-models-expand-depth: 5
show-actuator: true
group-configs:
- group: 用户中心模块
paths-to-match:
- /user/**
- group: 消息中心模块
paths-to-match:
- /msg/**
cache:
disabled: true
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.context.annotation.Configuration;
@Configuration
@OpenAPIDefinition(
info = @Info(
title = "Spring Boot 3 + Swagger 3 API文档",
description = "使用spring boot 3 和Swagger 3 构建生成的API文档",
version = "v1.0.0"
)
)
public class Swagger3Config {
}
数据对象
@Schema(description = "用户信息")
@Data
@Builder
public class UserDTO {
@Schema(description = "用户id",title = "用户id")
private Long id;
@Schema(description = "账户")
private String username;
@Schema(description = "密码")
private String password;
@Schema(description = "邮箱")
private String email;
@Schema(description = "电话")
private String phone;
@Schema(description = "启用状态")
private Boolean enabled;
@Schema(description = "注册时间")
private Date createTime;
}
接口
@Tag(name = "用户业务处理con",description = "更多详细描述.......")
@RestController
public class UserController {
@Operation(summary = "用户信息列表(接口简介)",description = "接口详细描述哟",tags = "用户业务处理con",
parameters = {
@Parameter(name = "Authorization",description = "Bearer token令牌",in = ParameterIn.HEADER),
@Parameter(name = "pageNum",description = "当前页码",required = true,example = "1"),
@Parameter(name = "pageSize",description = "分页大小",required = true,example = "1"),
},
responses = {
@ApiResponse(responseCode = "200", description = "成功返回")
}
)
@GetMapping("/user/getUsers")
public List<UserDTO> getUsers(int pageNum, int pageSize) {
List<UserDTO> users = new ArrayList<>();
users.add(UserDTO
.builder()
.id(1L)
.username("admin")
.password("000000")
.email("123@qqq.com")
.phone("1859999999")
.enabled(true)
.createTime(new Date())
.build()
);
return users;
}
@Operation(summary = "用户信息删除",description = "传入一个用户id,删除它",
parameters = {
@Parameter(name = "Authorization",description = "Bearer token令牌",in = ParameterIn.HEADER),
@Parameter(name = "userId",description = "用户id",required = true,in = ParameterIn.PATH),
}
)
@DeleteMapping("/user/{userId}")
public Boolean delete(@PathParam("userId") Long userId) {
return Objects.nonNull(userId);
}
}
启动项目,打开地址: http://localhost:8080 自动跳转 http://localhost:8080/swagger-ui/index.html 这是因为我们在application.yml增加了配置
如果觉得原生ui不好看,可以替换国内的knife4j
maven添加ui的依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-ui</artifactId>
<version>4.5.0</version>
</dependency>
重启项目并打开knife4j的ui地址 :http://localhost:8080/doc.html
如果考虑使用knife4j 增强功能则需要引入knife4j的starter并移除springdoc的starter,谨防冲突。
https://www.leftso.com/article/2408231727599086.html