swagger 接口文档

接入调试

1. 访问 swagger-ui 页面

跨域说明

请求入口必须是 pigx-gateway:9999 这种形式, 而不是 ip:9999 ,不然跨域。

2. 模拟登陆

模拟登陆界面

3. 填写客户端信息

默认账号信息

测试环境默认账号信息如下,生产环境请及时修改密码和客户端信息。

username: admin
password: JFat0Zdc
client_id: test
client_secret: test
填写客户端信息

新服务接入

1. 服务添加依赖

<dependency>
    <groupId>com.pig4cloud</groupId>
    <artifactId>pigx-common-swagger</artifactId>
</dependency>

2. 目标服务开启 OpenApi

注意事项

在应用主类中增加 @EnableOpenApi 注解,并且填写当前应用的网关请求前缀。

@EnableOpenApi("admin")
public class PigxAdminApplication {}

3. 接口增加 @SecurityRequirement

必须配置

Controller 接口上必须增加 @SecurityRequirement(name = HttpHeaders.AUTHORIZATION) 注解。

@Tag(description  = "connect", name =  "开放互联")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ConnectController {}

4. 重启网关服务(微服务版本)

重要操作

如果使用 PigX 微服务版本,需要在上述操作完成后重启 pigx-gateway 服务,以重新加载 Swagger 服务列表。

5. 常用注解

详细注解可以参考:Swagger 常用注解说明

Swagger 常用注解

关闭 Swagger 接口文档

方式一:配置文件关闭

在 nacos/application-dev.yml 配置文件中设置关闭属性:

springdoc:
  api-docs:
    enabled: false

方式二:代码配置关闭

在 SpringDocConfiguration 类中增加关闭配置:

SpringDocConfiguration 关闭配置