第 6 章 在 Spring Web 示例中启用 OpenAPI 和 Swagger-UI 支持


您可以通过添加 quarkus-smallrye-openapi 扩展,为您的应用程序添加 Swagger-UI 生成 OpenAPI 模式文档的支持。

流程

  1. 输入以下命令添加 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>

  2. 输入以下命令从 /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
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.