Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache CXF client loads fine in Eclipse but standalone jar throws NullpointerException in WSDLServiceFactory

My goal is to create a Web Service client that runs in a standalone jar with all the dependencies using mvn assembly:single

I generated the client using CXF codegen wsdl2java, creating a @WebServiceClient called NetBanxAutostatementService

For the dependencies I have

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

desperately I even tried to add more "stuff"

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

The problem: everytime I try to run "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)

This happens in the line that invokes the WebServiceClient autostatementService = new NetBanxAutostatementService(autostatement_wsdlLocation); I know by the log line that I am not passing autostatement_wsdlLocation as null

Java code:

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

}

like image 279
André Ricardo Avatar asked Jan 30 '12 18:01

André Ricardo


2 Answers

Most likely it's how you are creating your single jar. A normal usage of the assembly plugin would not allow that as various parts of CXFs META-INF/* stuff would need to be merged together. That would include all the /META-INF/spring* and much of the stuff in /META-INF/cxf/* I would suggest using the shade plugin for that. See the pom.xml for CXF's bundle jar for an example.

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

like image 141
Daniel Kulp Avatar answered Sep 19 '22 14:09

Daniel Kulp


Expanding on @DanielKulp's answer, which worked a treat for me with CXF 2.7.7 (just in case the link dies). Configure your shade plugin with the following additional transformers:

<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>
like image 20
Matt R Avatar answered Sep 19 '22 14:09

Matt R