2.91. 平台 HTTP


此扩展允许创建 HTTP 端点来消耗 HTTP 请求。

它基于 quarkus-vertx-http 扩展提供的 Eclipse Vert.x HTTP 服务器构建。

2.91.1. 什么是内部

有关用法和配置详情,请参阅上述链接。

2.91.2. Maven 协调

在 code.quarkus.redhat.com 上使用此扩展创建新项目

或者将协调添加到现有项目中:

<dependency>
    <groupId>org.apache.camel.quarkus</groupId>
    <artifactId>camel-quarkus-platform-http</artifactId>
</dependency>

2.91.3. 使用方法

2.91.3.1. 基本用法

/hello 端点上提供所有 HTTP 方法:

from("platform-http:/hello").setBody(simple("Hello ${header.name}"));

仅在 /hello 端点上服务 GET 请求:

from("platform-http:/hello?httpMethodRestrict=GET").setBody(simple("Hello ${header.name}"));

2.91.3.2. 通过 Camel REST DSL 使用 platform-http

要能够将 Camel REST DSL 与 platform-http 组件搭配使用,请将 camel-quarkus-rest 添加到 pom.xml 中:

<dependency>
    <groupId>org.apache.camel.quarkus</groupId>
    <artifactId>camel-quarkus-rest</artifactId>
</dependency>

然后您可以使用 Camel REST DSL:

rest()
    .get("/my-get-endpoint")
        .to("direct:handleGetRequest");

    .post("/my-post-endpoint")
        .to("direct:handlePostRequest");

2.91.3.3. 处理 multipart/form-data 文件上传

您可以通过白名单将上传限制到某些文件扩展:

from("platform-http:/upload/multipart?fileNameExtWhitelist=adoc,txt&httpMethodRestrict=POST")
    .to("log:multipart")
    .process(e -> {
        final AttachmentMessage am = e.getMessage(AttachmentMessage.class);
        if (am.hasAttachments()) {
            am.getAttachments().forEach((fileName, dataHandler) -> {
                try (InputStream in = dataHandler.getInputStream()) {
                    // do something with the input stream
                } catch (IOException ioe) {
                    throw new RuntimeException(ioe);
                }
            });
        }
    });

2.91.3.4. 保护 platform-http 端点

Quarkus 提供了各种安全和验证机制,它们可用于保护 platform-http 端点。详情请参考 Quarkus 安全 文档

在路由中,可以获取经过身份验证的用户及其关联的 SecurityIdentityPrincipal

from("platform-http:/secure")
    .process(e -> {
        Message message = e.getMessage();
        QuarkusHttpUser user = message.getHeader(VertxPlatformHttpConstants.AUTHENTICATED_USER, QuarkusHttpUser.class);
        SecurityIdentity securityIdentity = user.getSecurityIdentity();
        Principal principal = securityIdentity.getPrincipal();
        // Do something useful with SecurityIdentity / Principal. E.g check user roles etc.
    });

另外,请参阅 Quarkus 文档 esp 中的 quarkus.http.body ClusterClaim 配置选项。quarkus.http.body.handle-file-uploads,quarkus.http.body.uploads-directoryquarkus.http.body.delete-uploaded-files-on-end.

2.91.3.5. 实施反向代理

平台 HTTP 组件可以充当反向代理,在这种情况下,Exchange.HTTP_URIExchange.HTTP_HOST 标头是从 HTTP 请求请求行上收到的绝对 URL 填充的。

下面是一个 HTTP 代理的示例,该代理只是将 Exchange 重定向到原始服务器。

from("platform-http:proxy")
    .toD("http://"
        + "${headers." + Exchange.HTTP_HOST + "}");

2.91.3.6. 错误处理

如果您需要自定义从路由抛出异常时返回到客户端的存储库,您可以使用 Camel 错误处理行为,如 doTrydoCatchonException

例如,若要配置全局异常处理程序,以响应被抛出的特定 Exception 类型。

onException(InvalidOrderTotalException.class)
    .handled(true)
    .setHeader(Exchange.HTTP_RESPONSE_CODE).constant(500)
    .setHeader(Exchange.CONTENT_TYPE).constant("text/plain")
    .setBody().constant("The order total was not greater than 100");

from("platform-http:/orders")
    .choice().when().xpath("//order/total > 100")
        .to("direct:processOrder")
    .otherwise()
        .throwException(new InvalidOrderTotalException());

您可以使用 CDI 观察程序在 Vert.x Web 路由器初始化中的 hook 来实施更为精细的错误处理。

void initRouter(@Observes Router router) {
    // Custom 404 handler
    router.errorHandler(404, new Handler<RoutingContext>() {
        @Override
        public void handle(RoutingContext event) {
            event.response()
                .setStatusCode(404)
                .putHeader("Content-Type", "text/plain")
                .end("Sorry - resource not found");
        }
    });
}

请注意,当出现扩展(如 RestEASY )时,应谨慎修改路由器配置,因为它们可能会注册自己的错误处理逻辑。

2.91.4. 其他 Camel Quarkus 配置

2.91.4.1. 平台 HTTP 服务器配置

平台 HTTP 服务器配置由 Quarkus 管理。有关配置选项的完整列表,请参阅 Quarkus HTTP 配置指南

要为平台 HTTP 服务器配置 SSL,请遵循 SSL 指南 的安全连接。请注意,目前不支持使用 SSLContextParameters 为 SSL 配置服务器。

2.91.4.2. 字符编码

如果您希望应用程序使用非默认编码发送或接收请求,请参阅 Native 模式指南的 Character encodings 部分

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部