在Apache CXF和Java 11中创建SOAP客户端时出错

332nm8kg  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(184)

简而言之:

我正在尝试创建一个SOAP客户端,使用WSDL文件自动生成的代码。文件的生成使用CXF 3.5.3 Wsdl2Java。从WSDL生成数据模型的命令是:./wsdl2java -d ~/PROJECT/src/main/java -autoNameResolution PATH_TO_WSDL_FILE.wsdl
尝试运行代码时,我看到以下异常:

Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:345)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:343)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:343)
    at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:245)
    at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:499)
    at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:358)
    ... 8 more

在呼吁:PortClass port = (PortClass) factoryBean.create();
完整代码示例:

ClientProxyFactoryBean factoryBean = new ClientProxyFactoryBean();
factoryBean.setServiceClass(PortClass.class);
factoryBean.setAddress(ADDRESS);
PortClass port = (PortClass) factoryBean.create();

你知道如何正确设置依赖项吗?

完整工作流程:

起初,我使用OOB生成的文件,具有以下Gradle依赖项:

dependencies {
    implementation 'org.apache.cxf:cxf-rt-transports-http:3.5.3'
    implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.5.3'
    implementation 'org.apache.cxf:cxf-rt-ws-security:3.5.3'
    implementation 'javax.xml.soap:javax.xml.soap-api:1.4.0'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    implementation 'com.sun.xml.ws:jaxws-rt:4.0.0'
    implementation 'com.sun.xml.ws:jaxws-ri:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-ri:4.0.0'
    implementation 'com.sun.xml.ws:rt:4.0.0'
    implementation 'javax.xml.ws:jaxws-api:2.3.1'
    implementation 'javax.jws:javax.jws-api:1.1'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'javax.xml.ws:jaxws-api:2.3.1'
    implementation 'com.sun.xml.bind:jaxb-impl:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-core:4.0.0'
}

并遇到异常:Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
了解到在Java 11中您必须迁移到Jakarta而不是Javax,我将Gradle依赖项替换为:

dependencies {
    implementation 'org.apache.cxf:cxf-rt-transports-http:3.5.3'
    implementation 'jakarta.xml.soap:jakarta.xml.soap-api:3.0.0'
    implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.5.3'
    implementation 'com.sun.xml.bind:jaxb-impl:4.0.0'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'jakarta.jws:jakarta.jws-api:3.0.0'
    implementation 'jakarta.xml.ws:jakarta.xml.ws-api:4.0.0'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'com.sun.activation:javax.activation:1.2.0'
    implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
    implementation 'org.apache.cxf:cxf-rt-ws-security:3.5.3'
    implementation 'org.glassfish.metro:webservices-rt:4.0.1'
    implementation 'org.glassfish.metro:webservices-api:4.0.1'
    implementation 'com.sun.xml.messaging.saaj:saaj-impl:3.0.0'
    implementation 'com.sun.xml.ws:jaxws-rt:4.0.0'
    implementation 'com.sun.xml.ws:jaxws-ri:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-ri:4.0.0'
    implementation 'com.sun.xml.ws:rt:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-core:4.0.0'
}

接下来,生成的代码没有编译,因为在自动生成的文件中无法识别Jaxb(因为它不是依赖项的一部分):

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

因此,我手动将这些导入替换为:

import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
import jakarta.xml.bind.annotation.XmlSeeAlso;
import jakarta.xml.ws.RequestWrapper;
import jakarta.xml.ws.ResponseWrapper;

事实上,所有内容都可以编译,但仍然会出现错误,因为Apache CXF本身是基于javax构建的。我不得不将javax依赖项返回到Gradle中:

dependencies {
    implementation 'jakarta.xml.soap:jakarta.xml.soap-api:3.0.0'
    implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.5.3'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'jakarta.jws:jakarta.jws-api:3.0.0'
    implementation 'jakarta.xml.ws:jakarta.xml.ws-api:4.0.0'
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'org.apache.cxf:cxf-rt-ws-security:3.5.3'
    implementation 'org.glassfish.metro:webservices-rt:4.0.1'
    implementation 'org.glassfish.metro:webservices-api:4.0.1'
    implementation 'com.sun.xml.messaging.saaj:saaj-impl:3.0.0'
    implementation 'jakarta.xml.soap:jakarta.xml.soap-api:3.0.0'
    
    implementation 'javax.jws:javax.jws-api:1.1'
    implementation 'javax.activation:activation:1.1.1'
    implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-core:4.0.0'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'javax.xml.ws:jaxws-api:2.3.1'
    implementation 'javax.xml.soap:javax.xml.soap-api:1.4.0'
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'com.sun.activation:javax.activation:1.2.0'
    implementation 'org.apache.cxf:cxf-rt-transports-http:3.5.3'
    implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-impl:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-ri:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-core:4.0.0'
    implementation 'com.sun.xml.ws:jaxws-rt:4.0.0'
    implementation 'com.sun.xml.ws:jaxws-ri:4.0.0'
    implementation 'com.sun.xml.bind:jaxb-ri:4.0.0'
    implementation 'com.sun.xml.ws:rt:4.0.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

现在我得到了上面的异常:

Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:345)
    at org.apache.cxf.common.jaxb.JAXBContextCache$2.run(JAXBContextCache.java:343)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.apache.cxf.common.jaxb.JAXBContextCache.createContext(JAXBContextCache.java:343)
    at org.apache.cxf.common.jaxb.JAXBContextCache.getCachedContextAndSchemas(JAXBContextCache.java:245)
    at org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:499)
    at org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:358)
    ... 8 more
ar7v8xwq

ar7v8xwq1#

您使用的是Java 9(或更高版本)吗?此版本不再包含JAXB API。您可以查看How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

相关问题