4.5. OpenAPI 集成

概述

您可以使用 OpenAPI 服务为 CamelContext 文件中的任何 REST 定义路由和端点创建 API 文档。要做到这一点,使用 Camel REST DSL 及 camel-openapi-java 模块,它完全基于 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 在根路径中输出 CamelContext ID,例如 /api-docs,作为 JSON 格式的名称列表。要访问 OpenAPI 生成的文档,请在 CamelContext ID 中附加 REST 上下文路径,如 api-docs/myCamel。您可以使用 apiContextIdPattern 选项过滤此输出列表中的名称。

apiContextIdPattern

字符串

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

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)。这只为查看 REST API 文档启用 CORS,而不可用于访问 REST 服务。默认值为 false。建议改为使用 CorsFilter 选项,如此表中所述。

主机

字符串

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

方案

字符串

要使用的协议方案。使用逗号分隔多个值,如 "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 分发中,cal-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 路径参数的信息:

<!-- 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})")

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

<!-- 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 发行版中 的示例/camel-example-servlet-rest-tomcat