Apache CXF客户端在Eclipse中加载正常,但独立的jar在WSDLServiceFactory中抛出NullpointerException

时间:2012-01-30 18:52:09

标签: java web-services cxf webservice-client

我的目标是使用mvn程序集创建一个在独立jar中运行的Web服务客户端,其中包含所有依赖项: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传递为null

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);

}

4 个答案:

答案 0 :(得分:23)

很可能是你创建单个jar的方式。正常使用程序集插件不允许这样,因为需要将CXF的各种部分META-INF / *东西合并在一起。这将包括所有/ META-INF / spring *以及/ META-INF / cxf / *中的大部分内容我建议使用shade插件。有关示例,请参阅pom.xml以获取CXF的bundle jar。

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

答案 1 :(得分:20)

扩展@ 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>

答案 2 :(得分:3)

你应该尝试使用一个jar插件,它让你创建干净且完全独立的超级罐子

http://code.google.com/p/onejar-maven-plugin/

答案 3 :(得分:0)

作为对@Matt R的回答的补充,我用这段代码用maven-assembly-pluin替换了POM部分:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.xxx.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <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>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

排除部分是为了防止出现Java安全问题。