Java 11+Apache CXF创建SOAP客户端时JAXB依赖异常问题
解决Apache CXF 3.5.3 SOAP客户端JAXB依赖异常问题
问题根源
Java 9及以上版本移除了JDK内置的JAXB实现,而CXF 3.5.x基于javax命名空间开发,未迁移到Jakarta,盲目切换Jakarta依赖会导致组件不兼容,这是你遇到问题的核心原因。
正确依赖配置(Maven)
在pom.xml中添加以下依赖,确保CXF核心组件和JAXB的API、实现都被正确引入:
<!-- CXF SOAP客户端核心依赖 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.5.3</version> </dependency> <!-- JAXB API与实现依赖 --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.6</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <!-- 可选:处理XML Schema时需要 --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>
Gradle 对应配置
如果使用Gradle,在build.gradle中添加:
implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.5.3' implementation 'org.apache.cxf:cxf-rt-transports-http:3.5.3' implementation 'javax.xml.bind:jaxb-api:2.3.1' runtimeOnly 'com.sun.xml.bind:jaxb-impl:2.3.6' implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1' implementation 'javax.activation:activation:1.1.1'
关键注意事项
- 不要手动修改wsdl2java生成代码的导入语句,CXF 3.5.x生成的代码基于
javax.xml.bind等包,强行替换为Jakarta包会导致与CXF核心组件不兼容。 - 若使用Java 9+模块化项目(含
module-info.java),需在模块声明中添加依赖:requires javax.xml.bind; requires com.sun.xml.bind; requires org.apache.cxf.frontend.jaxws; requires org.apache.cxf.transports.http; - 清理项目缓存(Maven执行
clean install,Gradle执行clean build)后重新运行客户端代码。
内容的提问来源于stack exchange,提问作者Guy Yafe
相关产品推荐
相关产品推荐

