You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Gateway集成springdoc-openapi:网关API端点未显示求助

问题解决方法

1. 补充必要依赖

Spring Cloud Gateway基于WebFlux,需引入适配的springdoc依赖才能正确生成并聚合后端服务的OpenAPI文档。若使用Maven,在pom.xml中添加:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webflux-api-docs</artifactId>
    <version>2.2.0</version> <!-- 使用最新稳定版 -->
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
    <version>2.2.0</version>
</dependency>

2. 修正网关配置

2.1 补全URI协议头

原配置中uri缺少http://协议前缀,会导致路由转发失效,修正为:

uri: "http://localhost:8090/api/test/v1"

2.2 调整Path断言(可选)

若需匹配所有/sa开头的路径,将Path=/sa改为Path=/sa/**,否则仅精确匹配/sa路径:

predicates:
  - Path=/sa/**

2.3 配置springdoc聚合后端OpenAPI

要让网关的OpenAPI文档包含后端服务的接口信息,需启用网关的springdoc支持,并指定后端服务的API文档地址(假设后端服务已暴露/api-docs):

spring:
  cloud:
    gateway:
      routes:
        - id: testapi
          uri: "http://localhost:8090/api/test/v1"
          predicates:
            - Path=/sa/**
          filters:
            - AddRequestHeader=secure,true
  application:
    name: gateway-service
server:
  port: 8080
springdoc:
  api-docs:
    path: /api-docs
    server-url: http://localhost:8080 # 自定义网关的服务器URL(可选)
  gateway:
    enabled: true # 启用网关的OpenAPI聚合功能
    routes:
      testapi: # 对应路由id
        url: http://localhost:8090/api-docs # 后端服务的API文档地址

3. 验证效果

重启网关服务后,访问localhost:8080/api-docs,返回的JSON中paths会包含后端服务的接口信息,服务器URL也会正确显示网关地址。

原配置无效原因

  • uri缺少协议头,路由无法正确转发到后端服务;
  • 未配置springdoc的网关聚合功能,网关自身无接口,导致paths为空;
  • Path断言仅设置/sa时,仅匹配精确路径,可能无法覆盖实际需要转发的接口路径。

内容的提问来源于stack exchange,提问作者Chandrasekhar boya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 04:15:33