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

如何在Swagger YAML文件中声明多个端口?generator-express-no-stress环境下双端口API文档配置求助

Alright, let's get your dual-port Swagger documentation sorted out. Since you're working with Swagger 2.0 and need to document APIs on both 8080 and 9090 in the same YAML file, here's how to adjust your setup step by step:

Step 1: Add Multi-Server Support (for port switching)

Swagger 2.0 doesn't have native multi-host support, but Swagger UI recognizes the x-servers extension to let users toggle between your two ports. Remove the existing host and basePath lines from your YAML, and replace them with this:

x-servers:
  - url: http://ip:8080/api/v1
    description: API Service running on port 8080
  - url: http://ip:9090/api/v1
    description: API Service running on port 9090

Step 2: Tag APIs by Port for Clarity

Update your tags section to separate the two port's APIs—this makes navigation way easier in the Swagger UI:

tags:
  - name: Explorer (8080)
    description: APIs for explorer running on port 8080
  - name: Explorer (9090)
    description: APIs for explorer running on port 9090

Step 3: Add Your 9090 Port API Paths

Append the endpoints for your 9090 port API, assigning each to the new Explorer (9090) tag. I've included example paths here, but swap them out for your actual endpoints:

swagger: "2.0"
info:
  version: 1.0.0
  title: DIVI
  description: My cool app
x-servers:
  - url: http://ip:8080/api/v1
    description: API Service running on port 8080
  - url: http://ip:9090/api/v1
    description: API Service running on port 9090
tags:
  - name: Explorer (8080)
    description: APIs for explorer running on port 8080
  - name: Explorer (9090)
    description: APIs for explorer running on port 9090
schemes:
  - https
  - http
consumes:
  - application/json
produces:
  - application/json
definitions:
  ExampleBody:
    type: object
    title: example
    required:
      - name
    properties:
      name:
        type: string
        example: no_stress
paths:
  # Existing 8080 API paths
  /get-data:
    get:
      tags:
        - Explorer (8080)
      description: Fetch latest data (port 8080)
      responses:
        200:
          description: Returns all examples
  /get-version-data/{version}:
    get:
      tags:
        - Explorer (8080)
      parameters:
        - name: version
          in: path
          required: true
          description: The version of the explorer to retrieve
          type: integer
      responses:
        200:
          description: Return the example with the specified id
        404:
          description: Example not found
  /get-data/{search}:
    get:
      tags:
        - Explorer (8080)
      parameters:
        - name: search
          in: path
          required: true
          description: The search of the explorer to retrieve
          type: string  # Added missing type for validation
      responses:
        200:
          description: Return the example with the specified id
        404:
          description: Example not found

  # New 9090 API paths (customize these to match your actual endpoints)
  /get-service-status:
    get:
      tags:
        - Explorer (9090)
      description: Fetch service health status (port 9090)
      responses:
        200:
          description: Returns service status details
  /update-settings:
    post:
      tags:
        - Explorer (9090)
      description: Update application settings (port 9090)
      parameters:
        - name: settings
          in: body
          required: true
          schema:
            type: object
            properties:
              refreshInterval:
                type: integer
                example: 60000
      responses:
        200:
          description: Settings updated successfully
        400:
          description: Invalid settings data

Quick Note

I added a type: string to your /get-data/{search} path parameter since it was missing—Swagger 2.0 requires explicit types for path parameters to validate correctly.

Once you load this updated YAML into Swagger UI, you'll see a dropdown menu at the top to switch between the 8080 and 9090 servers. All your endpoints will be grouped under their respective port tags, so you can easily view and test APIs for either port without switching files.

内容的提问来源于stack exchange,提问作者Puneet Jindal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 12:42:38