【发布时间】:2021-12-18 12:02:52
【问题描述】:
我正在尝试使用 Swagger/Flask/Connexion 构建一个简单的 API,但是即使在按照指定 here 安装 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 方法也是如此。有任何想法吗?我一直在用头撞砖墙。
【问题讨论】:
标签: api flask swagger openapi connexion