You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wildfly 18 + Java 11环境下java.net.http.HttpResponse类找不到求助

问题描述

部署在Wildfly 18.0.1.Final上的JEE应用(基于OpenJDK 64-Bit Server VM 11.0.15+9-LTS),在使用java.net.http.HttpResponse$BodyHandler时触发ClassNotFoundException,直接调用该类也会报错。

尝试在WAR包的WEB-INF/jboss-deployment-structure.xml中添加模块依赖,但未生效,配置内容如下:

<jboss-deployment-structure>
  <module name="deployment.java.net.http" />
</jboss-deployment-structure>

相关堆栈跟踪末尾信息:

Caused by: java.lang.NoClassDefFoundError: java/net/http/HttpResponse$BodyHandler        
    at deployment.orbis-events-4u.war//io.apicurio.registry.rest.client.impl.RegistryClientImpl.<init>(RegistryClientImpl.java:67)
    at deployment.orbis-events-4u.war//io.apicurio.registry.rest.client.impl.RegistryClientImpl.<init>(RegistryClientImpl.java:63)
    at deployment.orbis-events-4u.war//io.apicurio.registry.rest.client.RegistryClientFactory.create(RegistryClientFactory.java:34)
    at deployment.orbis-events-4u.war//io.apicurio.registry.serde.AbstractSchemaResolver.configure(AbstractSchemaResolver.java:84)
    at deployment.orbis-events-4u.war//io.apicurio.registry.serde.DefaultSchemaResolver.configure(DefaultSchemaResolver.java:59)
    at deployment.orbis-events-4u.war//io.apicurio.registry.serde.SchemaResolverConfigurer.configure(SchemaResolverConfigurer.java:75)
    at deployment.orbis-events-4u.war//io.apicurio.registry.serde.AbstractKafkaSerDe.configure(AbstractKafkaSerDe.java:68)
    at deployment.orbis-events-4u.war//io.apicurio.registry.serde.avro.AvroKafkaSerializer.configure(AvroKafkaSerializer.java:81)
    at deployment.orbis-events-4u.war//org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:375)
    ... 62 more
Caused by: java.lang.ClassNotFoundException: java.net.http.HttpResponse$BodyHandler from [Module "deployment.orbis-events-4u.war" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:255)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:410)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:116)
    ... 71 more

疑问:java.net.http模块为何无法默认访问?是否有可行的应用配置方案?这是Wildfly的已知问题吗?

解决方案

1. 修正jboss-deployment-structure.xml配置

之前的配置存在两个关键问题:

  • 模块名错误:deployment.java.net.http是部署自身的模块标识,而java.net.http是JDK系统模块,需直接引用该模块名
  • 缺少必要节点结构:依赖必须放在<deployment>下的<dependencies>节点中

正确的配置如下:

<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="java.net.http" export="true" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

将该配置文件放在WAR包的WEB-INF目录下,重新部署应用即可生效。

2. 原因说明

这不是Wildfly的bug,而是其模块隔离机制的预期行为。Wildfly为保证兼容性和部署隔离,默认仅向应用暴露部分核心JDK模块,java.net.http是JDK 11新增的HTTP客户端模块,不在默认暴露列表中,因此需要显式声明依赖。

3. 全局暴露替代方案(可选)

如果有多个应用需要访问java.net.http模块,可通过Wildfly全局配置实现:

  • 在Wildfly安装目录的modules/system/layers/base/java/net/http/main目录(不存在则创建)下添加module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module name="java.net.http" xmlns="urn:jboss:module:1.6">
    <resources>
        <resource-root path="."/>
    </resources>
    <dependencies>
        <module name="java.base"/>
        <module name="java.logging"/>
        <module name="java.net"/>
    </dependencies>
</module>
  • 在standalone.xml或domain.xml的ee子系统配置中添加全局模块:
<subsystem xmlns="urn:jboss:domain:ee:4.0">
    <!-- 保留原有配置 -->
    <global-modules>
        <module name="java.net.http" slot="main"/>
    </global-modules>
</subsystem>

此配置会让所有部署的应用都能访问java.net.http模块。

内容的提问来源于stack exchange,提问作者zapho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 22:01:37