api - 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 方法也是如此。有任何想法吗?我一直在用头撞砖墙。
解决方案
您使用了错误的补丁,要访问 Swagger UI,您必须{base_path}/ui/
在您的情况下访问0.0.0.0:8080/ui/
您可以查看文档https://connexion.readthedocs.io/en/latest/quickstart.html#the-swagger-ui-console
推荐阅读
- javascript - JavaScript:如何按名称获取变量的值
- r - 使用 grid.arrange 在绘图上方添加额外的空间作为标题
- flutter - 注册firebase Flutter后无法导航到主页
- html - 自动完成不适用于原子重新安装
- javacc - Qbasic 语言的 JavaCC 中的错误恢复
- php - 同一 Slash 路由之间的多个参数
- .htaccess - 十月 CMS:尽管重定向,http://localhost/backend 返回 404,而 http://localhost/index.php/backend 有效
- laravel - 这个人的申请是否被批准
- r - 从列表向量monitoR中提取S4对象
- python - 如何从 API 请求中提取数字?