搜索

4.5. OpenAPI 集成

download PDF

概述

您可以使用 OpenAPI 服务为 CamelContext 文件中的任何 REST 定义路由和端点创建 API 文档。为此,请使用带有 camel-openapi-java 模块的 Camel REST DSL,该模块纯纯基于 Java。camel-openapi-java 模块会创建一个 servlet,它与 CamelContext 集成,并从每个 REST 端点拉取信息,以 JSON 或 YAML 格式生成 API 文档。

如果使用 Maven,请编辑 pom.xml 文件以添加对 camel-openapi-java 组件的依赖项:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-openapi-java</artifactId>
   <version>x.x.x</version>
   <!-- Specify the version of your camel-core module. -->
</dependency>

配置 CamelContext 以启用 OpenAPI

要在 Camel REST DSL 中使用 OpenAPI,请调用 apiContextPath () 来为 OpenAPI 生成的 API 文档设置上下文路径。例如:

public class UserRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        // Configure the Camel REST DSL to use the netty4-http component:
        restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json)
            // Generate pretty print output:
            .dataFormatProperty("prettyPrint", "true")
            // Set the context path and port number that netty will use:
            .contextPath("/").port(8080)
            // Add the context path for the OpenAPI-generated API documentation:
            .apiContextPath("/api-doc")
                .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
                // Enable CORS:
                .apiProperty("cors", "true");

        // This user REST service handles only JSON files:
        rest("/user").description("User rest service")
            .consumes("application/json").produces("application/json")
            .get("/{id}").description("Find user by id").outType(User.class)
                .param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
                .to("bean:userService?method=getUser(${header.id})")
            .put().description("Updates or create a user").type(User.class)
                .param().name("body").type(body).description("The user to update or create").endParam()
                .to("bean:userService?method=updateUser")
            .get("/findAll").description("Find all users").outTypeList(User.class)
                .to("bean:userService?method=listUsers");
    }
}

OpenAPI 模块配置选项

下表中描述的选项可让您配置 OpenAPI 模块。设置选项,如下所示:

  • 如果您使用 camel-openapi-java 模块作为 servlet,通过更新 web.xml 文件并为您要设置的每个配置选项指定一个 init-param 元素来设置选项。
  • 如果您使用 Camel REST 组件的 camel-openapi-java 模块,请通过调用适当的 RestConfigurationDefinition 方法来设置选项,如 enableCORS ()host ()contextPath ()。使用 RestConfigurationDefinition.apiProperty () 方法设置 api.xxx 选项。
选项类型描述

api.contact.email

字符串

用于与 API 相关的电子邮件地址。

api.contact.name

字符串

要联系的人员或机构的名称。

api.contact.url

字符串

网站 URL 以获得更多联系信息。

apiContextIdListing

布尔值

如果您的应用程序使用多个 CamelContext 对象,则默认行为是仅列出当前 CamelContext 中的 REST 端点。如果您希望运行 REST 服务的 JVM 中运行的每个 CamelContext 中的 REST 端点列表,则此选项设置为 true。当 apiContextIdListing 为 true 时,OpenAPI 会输出 root 路径中的 CamelContext ID,例如 /api-docs,作为 JSON 格式的名称列表。要访问 OpenAPI 生成的文档,请将 REST 上下文路径附加到 CamelContext ID 中,如 api-docs/myCamel。您可以使用 apiContextIdPattern 选项过滤此输出列表中的名称。

apiContextIdPattern

字符串

过滤的 CamelContext ID 出现在上下文列表中的模式。您可以指定正则表达式,并使用 * 作为通配符。这与 Camel Intercept 功能使用的模式匹配工具相同。

api.license.name

字符串

用于 API 的许可证名称。

api.license.url

字符串

用于 API 的许可证的 URL。

api.path

字符串

设置可用于生成文档的 REST API 的路径,例如 /api-docs。指定相对路径。不要指定如 httphttpscamel-openapi-java 模块在运行时计算绝对路径,格式为: protocol://host:port/context-path/api-path

api.termsOfService

字符串

API 服务术语的 URL。

api.title

字符串

应用程序的标题。

api.version

字符串

API 的版本。默认值为 0.0.0。

base.path

字符串

必需。设置 REST 服务可用的路径。指定相对路径。也就是说,不要指定 httphttpscamel-openapi-java modul 以以下格式在运行时计算绝对路径: protocol://host:port/context-path/base.path

CORS

布尔值

是否启用 HTTP 访问控制(CORS)。这只启用 CORS 以查看 REST API 文档,而不要访问 REST 服务。默认值为 false。建议是使用 CorsFilter 选项,如此表后所述。

主机

字符串

设置运行 OpenAPI 服务的主机的名称。默认是根据 localhost 计算主机名。

schemes

字符串

要使用的协议方案。使用逗号分隔多个值,例如 "http,https"。默认值为 http

opeapi.version

字符串

OpenAPI 规格版本。默认值为 3.0。

获取 JSON 或 YAML 输出

从 Camel 3.1 开始,camel-openapi-java 模块支持 JSON 和 YAML 格式的输出。要指定您想要的输出,请将 /openapi.json/openapi.yaml 添加到请求 URL。如果请求 URL 没有指定格式,则 camel-openapi-java 模块会检查 HTTP Accept 标头来检测是否可以接受 JSON 或 YAML。如果两者都被接受,或者没有设置为接受,则 JSON 是默认返回格式。

例子

在 Apache Camel 3.x 分发中,camel-example-openapi-cdicamel-example-openapi-java 演示了 camel-openapi-java 模块的使用。

在 Apache Camel 2.x 发行版中,camel-example-swagger-cdicamel-example-swagger-java 演示 camel-swagger-java 模块的使用。

增强 OpenAPI 生成的文档

从 Camel 3.1 开始,您可以通过定义名称、描述、数据类型、参数类型等参数详情来增强 OpenAPI 生成的文档。如果使用 XML,请指定 param 元素来添加此信息。以下示例演示了如何提供有关 ID path 参数的信息:

<!-- This is a REST GET request to view information for the user with the given ID: -->
<get uri="/{id}" outType="org.apache.camel.example.rest.User">
     <description>Find user by ID.</description>
     <param name="id" type="path" description="The ID of the user to get information about." dataType="int"/>
     <to uri="bean:userService?method=getUser(${header.id})"/>
</get>

以下是 Java DSL 中的相同示例:

.get("/{id}").description("Find user by ID.").outType(User.class)
   .param().name("id").type(path).description("The ID of the user to get information about.").dataType("int").endParam()
   .to("bean:userService?method=getUser(${header.id})")

如果您定义了名称为 body 的参数,那么您还必须将 body 指定为该参数的类型。例如:

<!-- This is a REST PUT request to create/update information about a user. -->
<put type="org.apache.camel.example.rest.User">
     <description>Updates or creates a user.</description>
     <param name="body" type="body" description="The user to update or create."/>
     <to uri="bean:userService?method=updateUser"/>
</put>

以下是 Java DSL 中的相同示例:

.put().description("Updates or create a user").type(User.class)
     .param().name("body").type(body).description("The user to update or create.").endParam()
     .to("bean:userService?method=updateUser")

另请参阅: Apache Camel 发行版中的 examples/camel-example-servlet-rest-tomcat

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.