2.62. 平台 HTTP


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

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

2.62.1. 内部是什么

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

2.62.2. Maven 协调

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

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

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

2.62.3. 使用方法

2.62.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.62.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.62.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);
                }
            });
        }
    });

2.62.3.4. 保护 platform-http 端点

Quarkus 提供了各种安全性和身份验证机制,可用于保护 platform-http 端点。如需更多详细信息,请参阅 Quarkus 安全文档

在路由中,可以获取经过身份验证的用户及其关联的安全Identity 主体

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 the 配置选项。quarkus.http.body.handle-file-uploads,quarkus.http.body.uploads-directoryquarkus.http.body.delete-uploaded-files-on-end.

2.62.3.5. 实现反向代理

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

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

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

2.62.4. 其他 Camel Quarkus 配置

2.62.4.1. 平台 HTTP 服务器配置

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

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

2.62.4.2. 字符编码

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

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部