Spring Boot 2.7.2(JDK18)集成SOAP接口报SOAPException缺失错误求助
问题分析与解决方案
错误根源
JDK 9及以上版本已移除javax.xml.soap相关API(原属于Java EE模块,被标记为废弃并移出JDK核心),而Spring Boot 2.7.x默认不会自动引入该类依赖,导致运行时无法找到SOAPException类。
1. 引入正确的SOAP API与实现依赖
在pom.xml中添加以下依赖,确保API与实现完整:
<dependency> <groupId>javax.xml.soap</groupId> <artifactId>javax.xml.soap-api</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>com.sun.xml.messaging.saaj</groupId> <artifactId>saaj-impl</artifactId> <version>1.5.3</version> <!-- 排除重复的API依赖,避免版本冲突 --> <exclusions> <exclusion> <groupId>javax.xml.soap</groupId> <artifactId>javax.xml.soap-api</artifactId> </exclusion> </exclusions> </dependency>
注:
saaj-impl是SOAP API的实际实现类库,1.5.3及以上版本兼容JDK 18。
2. 检查模块化配置(若启用模块)
如果项目使用Java模块化(存在module-info.java),需在模块声明中添加依赖:
requires javax.xml.soap.api; requires com.sun.xml.messaging.saaj;
3. 验证WebServiceConfig配置正确性
确保SOAP服务的路径映射与WSDL配置一致,示例配置如下:
@Configuration @EnableWs public class WebServiceConfig { @Bean public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); servlet.setTransformWsdlLocations(true); // 确保SOAP请求路径与WSDL的LocationUri一致 return new ServletRegistrationBean<>(servlet, "/soap/*"); } @Bean(name = "bnlService") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema bnlSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("BnlPort"); wsdl11Definition.setLocationUri("/soap"); wsdl11Definition.setTargetNamespace("http://your-custom-namespace.com/soap"); wsdl11Definition.setSchema(bnlSchema); return wsdl11Definition; } @Bean public XsdSchema bnlSchema() { return new SimpleXsdSchema(new ClassPathResource("bnl.xsd")); } }
重点确认:LocationUri与Servlet映射路径匹配,XSD文件路径正确可加载。
4. 排查依赖冲突
执行Maven命令查看依赖树,检查是否有其他依赖排除了SOAP相关包:
mvn dependency:tree | grep -E "soap|saaj"
若发现冲突,调整对应依赖的scope或添加exclusions排除冲突项。
内容的提问来源于stack exchange,提问作者Nico
相关产品推荐
相关产品推荐

