Apache CXF 客户端在 Eclipse 中加载良好,但独立 jar 在 WSDLServiceFactory 中抛出 NullpointerException

2022-09-02 11:18:39

我的目标是创建一个Web服务客户端,该客户端在一个独立的jar中运行,所有依赖项都使用mvn assembly:single

我使用CXF codegen wsdl2java生成了客户端,创建了一个名为NetBanxAutostatementService@WebServiceClient。

对于我拥有的依赖项

<cxf.version>2.5.2</cxf.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
        <scope>runtime</scope>
    </dependency>

我甚至拼命地试图添加更多的“东西”。

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-core</artifactId>
        <version>2.5.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf</artifactId>
        <version>2.5.2</version>
        <type>pom</type>
        <scope>runtime</scope>
    </dependency>

问题:每次我尝试运行“java -jar target/Netbanx-0.0.1-SNAPSHOT-jar-with-dependencies.jar”

INFO [main] (Netbanx.java:97) - autostatement_wsdlLocation:https://www.test.netbanx.com/cgi-bin/autostatement_wsdl
Exception in thread "main" java.lang.NullPointerException
at org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:92)
at org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:204)
at org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:148)
at org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:91)
at javax.xml.ws.Service.<init>(Service.java:56)
at com.netbanx.autostatement.NetBanxAutostatementService.<init>  (NetBanxAutostatementService.java:39)
at my.project.netbanx.Netbanx.<init>(Netbanx.java:98)
at my.project.netbanx.Netbanx.main(Netbanx.java:130)

这发生在调用WebServiceClient autostatementService = new NetBanxAutostatementService(autostatement_wsdlLocation)的行中;我通过日志行知道我没有将autostatement_wsdlLocation作为空值传递

Java 代码:

URL autostatement_wsdlLocation = null;
URL payment_wsdlLocation = null;
try {
    autostatement_wsdlLocation = new URL(properties.getProperty("autostatement_wsdlLocation"));
    payment_wsdlLocation = new URL(properties.getProperty("payment_wsdlLocation"));
} catch (MalformedURLException e) {
    logger.error("MalformedURLException",e);
}

    /**
     * Load the Netbanx's webservices AutostatementService and PaymentService
     */
try {           
    logger.info("autostatement_wsdlLocation:"+autostatement_wsdlLocation.toString());
    autostatementService = new NetBanxAutostatementService(autostatement_wsdlLocation); //it is here I get the NullPointerException error
    logger.info("payment_wsdlLocation:"+payment_wsdlLocation.toString());
    paymentService = new NetBanxPaymentService(payment_wsdlLocation);
webServiceStarted = true;
    } catch(javax.xml.ws.WebServiceException wsException ){
        String error = "Cannot create NetBanx web service please make sure this host can reach:" + autostatement_wsdlLocation +" and " + payment_wsdlLocation;
        logger.error(error);
        logger.error("WebServiceException",wsException);

}


答案 1

最有可能的是,这就是你创建单个罐子的方式。汇编插件的正常使用不允许这样做,因为CXFs META-INF / *内容的各个部分需要合并在一起。这将包括所有 /META-INF/spring* 和 /META-INF/cxf/* 中的许多内容,我建议使用 shade 插件。有关示例,请参阅cXF捆绑罐的pom.xml。

http://svn.apache.org/repos/asf/cxf/trunk/osgi/bundle/all/


答案 2

扩展@DanielKulp的答案,这对我使用CXF 2.7.7(以防万一链接死机)是一种享受。使用以下附加转换器配置遮阳插件:

<configuration>
  <transformers>
    <!--  transformers for CXF (see http://stackoverflow.com/a/9069435/61298) -->
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.handlers</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/services/com.sun.tools.xjc.Plugin</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.schemas</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/cxf/cxf.extension</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/cxf/bus-extensions.txt</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/bus-extensions.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/wsdl.plugin.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/tools.service.validator.xml</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
      <resource>META-INF/cxf/java2wsbeans.xml</resource>
    </transformer>
  </transformers>
</configuration>

推荐