首页 > 解决方案 > Connexion 不提供 Swagger UI

问题描述

我正在尝试使用 Swagger/Flask/Connexion 构建一个简单的 API,但是即使按照此处指定的方式安装了 connexion[swagger-ui],他们 GitHub 上的示例也不起作用

pip3 install 'connexion[swagger-ui]'

这是我的 helloworld-api.yaml:

openapi: "3.0.0"

info:
  title: Hello World
  version: "1.0"
servers:
  - url: http://localhost:9090/swagger

paths:
  /greeting:
    get:
      summary: Generate greeting
      description: Generates a greeting message.
      operationId: hello.get_greeting
      responses:
        200:
          description: greeting response
          content:
            text/plain:
              schema:
                type: string
                example: "hello dave!"
      parameters:
        - in: query
          name: name
          required: true
          schema:
            type: string
            example: "dave"
          description: Name of the person to greet.

还有我的 hello.py

#!/usr/bin/env python3

import connexion


def get_greeting(name: str) -> str:
    return f'Hello {name}'

if __name__ == '__main__':
    app = connexion.FlaskApp(__name__, specification_dir='openapi/')
    app.add_api('helloworld-api.yaml', arguments={'title': 'Hello World Example'})
    app.run(port=8080)

hello.py 位于根目录中,helloworld-api.yaml 位于名为 openapi/ 的文件夹中。在浏览器中导航到 0.0.0.0:8080/swagger 会返回以下内容:

{
  "detail": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
  "status": 404,
  "title": "Not Found",
  "type": "about:blank"
}

我指定的 get 方法也是如此。有任何想法吗?我一直在用头撞砖墙。

标签: apiflaskswaggeropenapiconnexion

解决方案


您使用了错误的补丁,要访问 Swagger UI,您必须{base_path}/ui/在您的情况下访问0.0.0.0:8080/ui/

您可以查看文档https://connexion.readthedocs.io/en/latest/quickstart.html#the-swagger-ui-console


推荐阅读