2.59. 平台 HTTP


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

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

2.59.1. 内部内容

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

2.59.2. Maven 协调

在 code.quarkus.redhat.com 上创建一个具有此扩展名的新项目

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

<dependency>
    <groupId>org.apache.camel.quarkus</groupId>
    <artifactId>camel-quarkus-platform-http</artifactId>
</dependency>
Copy to Clipboard Toggle word wrap

2.59.3. 使用方法

2.59.3.1. 基本用法

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

from("platform-http:/hello").setBody(simple("Hello ${header.name}"));
Copy to Clipboard Toggle word wrap

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

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

2.59.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>
Copy to Clipboard Toggle word wrap

然后,您可以使用 Camel REST DSL:

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

    .post("/my-post-endpoint")
        .to("direct:handlePostRequest");
Copy to Clipboard Toggle word wrap

2.59.3.3. 处理 多部分/数据 文件上传

您可以通过列出它们,将上传限制到特定的文件扩展:

from("platform-http:/upload/multipart?fileNameExtWhitelist=html,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);
                }
            });
        }
    });
Copy to Clipboard Toggle word wrap

2.59.3.4. 保护 platform-http 端点

Quarkus 提供了各种安全性和身份验证机制,可用于保护 平台-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.
    });
Copy to Clipboard Toggle word wrap

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

2.59.3.5. 实施反向代理

平台 HTTP 组件可以充当反向代理,本例中为 Exchange.HTTP_URIExchange.HTTP_HOST 标头由 HTTP 请求行中收到的绝对 URL 填充。

以下是 HTTP 代理的示例,它只是将 Exchange 重定向到原始服务器。

from("platform-http:proxy")
    .toD("http://"
        + "${headers." + Exchange.HTTP_HOST + "}");
Copy to Clipboard Toggle word wrap

2.59.4. 其他 Camel Quarkus 配置

2.59.4.1. 平台 HTTP 服务器配置

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

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

2.59.4.2. 字符编码

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

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat