第 6 章 在 Spring Web 示例中启用 OpenAPI 和 Swagger-UI 支持
您可以通过添加 quarkus-smallrye-openapi
扩展,为您的应用程序添加 Swagger-UI 生成 OpenAPI 模式文档的支持。
流程
输入以下命令添加
quarkus-smallrye-openapi
扩展作为 Spring Web 示例的依赖项。添加扩展足以从 REST 端点生成基本的 OpenAPI 模式文档:./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-smallrye-openapi"
输入命令将以下依赖项添加到您的
pom.xml
中:pom.xml
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-openapi</artifactId> </dependency>
输入以下命令从
/q/openapi
获取 schema 文档:curl http://localhost:8080/q/openapi
您可以使用 YAML 格式生成的 OpenAPI 模式文档收到响应:
--- openapi: 3.0.3 info: title: Generated API version: "1.0" paths: /greeting: get: responses: "200": description: OK content: text/plain: schema: type: string /greeting/{name}: get: parameters: - name: name in: path required: true schema: type: string responses: "200": description: OK content: application/json: schema: $ref: '#/components/schemas/Greeting' components: schemas: Greeting: type: object properties: message: type: string