SpringBoot使用Gateway聚合Springdoc,Knife4j

SpringBoot使用Gateway聚合Springdoc,Knife4j

前言

同时支持springboot:3.0,springboot:2.0,使用gateway聚合springdoc,ui使用knife4j,解决由于nginx配置代理前缀导致的文档无法访问,不强依赖注册中心(nacos,zk,Eureka)

有帮助的话记得点个赞哟!!!

基础环境

将所有依赖集成好作为一个本地包供其他项目使用

  • jdk17
  • maven3.6+
  • springboot3.0+|springboot2.0+
  • springcloud:2022.0.1
  • springcloud-alibaba:1.8.1-2022.0.0-RC2
  • springdoc:2.0.2|springdoc:1.6.0+
  • knife4j:4.0.0

当使用springboot:3.0,请使用 springdoc:2.0.0+
当使用springboot:2.0,请使用 springdoc:1.6.0+(部分springdoc的包名替换即可)

工具包(framework-document)

依赖

<dependencies>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webflux-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

代码

文档信息properties
import com.github.mpcloud.framework.core.consts.Constant;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * The type Swagger properties.
 *
 * @author : Milo
 */
@ConfigurationProperties("springdoc.info")
public class DocumentInfoProperties {
   
    private static final String DEFAULT_OPEN_API_ROUTE_NAME = "open-api-route";
    private static final String DEFAULT_GATEWAY_API_ROUTE_NAME = "open-api-gateway-route";
    /**
     * 文档标题
     */
    private String title;
    /**
     * 文档描述
     */
    private String description;
    /**
     * 项目version
     */
    private String projectVersion;
    /**
     * 许可证
     */
    private String license;
    /**
     * 许可证URL
     */
    private String licenseUrl;
    /**
     * 项目负责人信息
     */
    private final Contact contact = new Contact();
    /**
     * gateway
     */
    private final Gateway gateway = new Gateway();

    public String getTitle() {
   
        return this.title;
    }

    public void setTitle(final String title) {
   
        this.title = title;
    }

    public String getDescription() {
   
        return this.description;
    }

    public void setDescription(final String description) {
   
        this.description = description;
    }

    public String getProjectVersion() {
   
        return this.projectVersion;
    }

    public void setProjectVersion(final String projectVersion) {
   
        this.projectVersion = projectVersion;
    }

    public String getLicense() {
   
        return this.license;
    }

    public void setLicense(final String license) {
   
        this.license = license;
    }

    public String getLicenseUrl() {
   
        return this.licenseUrl;
    }

    public void setLicenseUrl(final String licenseUrl) {
   
        this.licenseUrl = licenseUrl;
    }

    public Contact getContact() {
   
        return this.contact;
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Gateway网关聚合knife4j 4.x是一个基于Spring Cloud Gateway的API网关聚合工具,可以方便地将多个微服务的API文档聚合在一起,并提供统一的API文档页面。它使用Knife4j 4.x作为API文档展示工具,支持多种文档格式展示,如Swagger、OpenAPI等。 使用Gateway网关聚合knife4j 4.x,可以大大简化API文档的维护工作,减少对多个微服务进行单独维护的工作量。同时,还可以提供一致的API文档展示风格,使得API文档更加易读易用。 要使用Gateway网关聚合knife4j 4.x,需要进行如下步骤: 1. 引入依赖:在Spring Cloud Gateway的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>${knife4j.version}</version> </dependency> ``` 2. 配置Swagger文档:在每个微服务的配置文件中,配置Swagger文档的相关信息,如下所示: ```yaml spring: application: name: user-service swagger: enabled: true title: User Service API description: API for User Service version: 1.0 base-package: com.example.user.api ``` 3. 配置Gateway网关:在Gateway网关的配置文件中,配置路由规则,并将Swagger文档的请求进行聚合,如下所示: ```yaml spring: cloud: gateway: routes: - id: user-service uri: lb://user-service predicates: - Path=/user/** filters: - RewritePath=/user/(?<path>.*), /$\{path} - StripPrefix=1 - id: api-docs uri: lb://user-service predicates: - Path=/v2/api-docs/** filters: - RewritePath=/v2/api-docs/(?<path>.*), /swagger-resources/configuration/ui - id: swagger-resources uri: lb://user-service predicates: - Path=/swagger-resources/** filters: - RewritePath=/swagger-resources/(?<path>.*), /swagger-resources/$\{path} - id: swagger-ui uri: lb://user-service predicates: - Path=/swagger-ui/** filters: - RewritePath=/swagger-ui/(?<path>.*), /swagger-ui/$\{path} ``` 4. 启动应用:启动应用后,访问Gateway网关的Swagger文档页面即可看到聚合后的API文档。 以上就是使用Gateway网关聚合knife4j 4.x的基本步骤,希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值