添加Swagger3依赖
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.3.1</version>
</dependency>
添加注解
OpenApi注解Swagger3
配置文件
server:
port: 8080
springdoc:
api-docs:
#是否开启文档功能
enabled: true
#swagger后端请求地址
path: /api-docs
swagger-ui:
#自定义swagger前端请求路径,输入http:127.0.0.1:8080/test会自动重定向到swagger页面
path: /test
#包扫描路径
packages-to-scan: com.example
#这里定义了两个分组,可定义多个,也可以不定义
group-configs:
#分组名
- group: admin
#按路径匹配
pathsToMatch: /admin/**
#分组名
- group: user
#按包路径匹配
packagesToScan: com.hello.api.user
controller层
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
@Tag(name = "测试接口管理-OpenApiSwagger3")
@RestController
@RequestMapping("/test")
public class TestController {
@Operation(summary = "测试接口")
@PostMapping("/getCarInfo")
public CarInfoResponseDTO test (@RequestBody CarInfoRequestDTO carInfoRequestDTO) {
return new CarInfoResponseDTO();
}
}
入参添加注解
// 入参添加注解
@Data
@Schema(name = "CarInfoRequestDTO", description = "车辆信息DTO")
public class CarInfoRequestDTO implements Serializable {
@Schema(name = "vin", description = "车架号", required = true)
private String vin;
@Schema(name = "plateNo", description = "车牌号", required = true)
private String plateNo;
@Schema(name = "plateType", description = "号牌种类", required = true)
private String plateType;
@Schema(name = "vehicleName", description = "厂牌型号", required = true)
private String vehicleName;
@Schema(name = "orderId", description = "订单id", required = true)
private String orderId;
}
返参添加注解
// 返参添加注解
@Data
@Schema(name = "CarInfoResponseDTO", description = "车辆信息响应DTO")
public class CarInfoResponseDTO {
@Schema(name = "vin", description = "车架码(vin码)", required = true)
private String vin;
@Schema(name = "plateNo", description = "车牌号", required = true)
private String plateNo;
@Schema(name = "engNO", description = "发动机号", required = true)
private String engNO;
}
运行项目
查看Swagger UI
注意图中的箭头所指的第一组的标记和上面配置类中的配置,你就明白了.
鉴于SwaggerUI真的不太方便,于是我们用Yapi来做接口的展示
yapiUI
这里配置的是SwaggerUI中json地址
更新接口
下面看一下YAPI上面接口的详情:
接口头部
接口的头部
入参和返参
相关链接
OpenApi和Swagger2 Swagger3的关系
Springboot集成Swagger2并通过Yapi做接口管理
Springboot集成springFox-Swagger3并通过Yapi做接口管理
Sofaboot集成Swagger3并通过Yapi做接口管理
swagger-yapi-demo项目下载
版权声明:本文为qq_15022971原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。