Search code examples
javasoapcxf-client

Can not initialize the default wsdl from classpath:wsdl


I'm following an example to understand how SOAP works. I have generated code from wsdl using Apache cxf and I can log SOAP web service request e response. Apparently all works fine. I have just a problem to set a relative path. I've followed this solution How to avoid the need to specify the WSDL location in a CXF or JAX-WS generated webservice client?, but there isn't way to solve the log error on console.

The error message on console:

jun 03, 2021 2:17:38 PM io.codejournal.maven.wsdl2java.NumberConversion <clinit>
INFO: Can not initialize the default wsdl from classpath:wsdl/dataaccess-numberconversion.wsdl

this is my pom.xml:

<properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>11</java.version>
      <maven.compiler.source>${java.version}</maven.compiler.source>
      <maven.compiler.target>${java.version}</maven.compiler.target>
   </properties>
   <dependencies>
      <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.3.3</version>
      </dependency>
     <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-simple</artifactId>
        <version>3.1.18</version>
    </dependency>         
  </dependencies>
   <build>  
      <plugins>
        <plugin>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-codegen-plugin</artifactId>
          <version>3.4.2</version>
          <executions>
            <execution>
              <id>generate-sources</id>
              <phase>generate-sources</phase>
              <goals>
                <goal>wsdl2java</goal>
              </goals>
              <configuration>
                <wsdlOptions>
                  <wsdlOption>
                    <wsdl>${basedir}/src/main/resources/dataaccess-numberconversion.wsdl</wsdl>
                    <wsdlLocation>classpath:wsdl/dataaccess-numberconversion.wsdl</wsdlLocation>
                    <packagenames>
                      <packagename>io.codejournal.maven.wsdl2java</packagename>
                    </packagenames>
                  </wsdlOption>
                </wsdlOptions>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>

My service class:

@WebServiceClient(name = "NumberConversion",
                  wsdlLocation = "classpath:wsdl/dataaccess-numberconversion.wsdl",
                  targetNamespace = "http://www.dataaccess.com/webservicesserver/")
public class NumberConversion extends Service {

    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://www.dataaccess.com/webservicesserver/", "NumberConversion");
    public final static QName NumberConversionSoap = new QName("http://www.dataaccess.com/webservicesserver/", "NumberConversionSoap");
    public final static QName NumberConversionSoap12 = new QName("http://www.dataaccess.com/webservicesserver/", "NumberConversionSoap12");
    static {
        URL url = NumberConversion.class.getClassLoader().getResource("wsdl/dataaccess-numberconversion.wsdl");
        if (url == null) {
            java.util.logging.Logger.getLogger(NumberConversion.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "classpath:wsdl/dataaccess-numberconversion.wsdl");
        }
        WSDL_LOCATION = url;
    }

    public NumberConversion(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public NumberConversion(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public NumberConversion() {
        super(WSDL_LOCATION, SERVICE);
    }

    public NumberConversion(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }

    public NumberConversion(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

    public NumberConversion(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }
   
    /**
     *
     * @return
     *     returns NumberConversionSoapType
     */
    @WebEndpoint(name = "NumberConversionSoap")
    public NumberConversionSoapType getNumberConversionSoap() {
        return super.getPort(NumberConversionSoap, NumberConversionSoapType.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns NumberConversionSoapType
     */
    @WebEndpoint(name = "NumberConversionSoap")
    public NumberConversionSoapType getNumberConversionSoap(WebServiceFeature... features) {
        return super.getPort(NumberConversionSoap, NumberConversionSoapType.class, features);
    }


    /**
     *
     * @return
     *     returns NumberConversionSoapType
     */
    @WebEndpoint(name = "NumberConversionSoap12")
    public NumberConversionSoapType getNumberConversionSoap12() {
        return super.getPort(NumberConversionSoap12, NumberConversionSoapType.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns NumberConversionSoapType
     */
    @WebEndpoint(name = "NumberConversionSoap12")
    public NumberConversionSoapType getNumberConversionSoap12(WebServiceFeature... features) {
        return super.getPort(NumberConversionSoap12, NumberConversionSoapType.class, features);
    }

}

When I run this code URL url = NumberConversion.class.getClassLoader().getResource("wsdl/dataaccess-numberconversion.wsdl"); , I'll get a null value back. It is the wrong path that NumberConversion.class.getClassLoader() calls, because this class is in another package.

Any idea how to set in the right way?


Solution

  • There is a mismatch in your <wsdlOption>, between the <wsdl> and <wsdlLocation> values.

    <wsdl>${basedir}/src/main/resources/dataaccess-numberconversion.wsdl</wsdl>
    <wsdlLocation>classpath:wsdl/dataaccess-numberconversion.wsdl</wsdlLocation>
    

    The first line says the file is in the root of the classpath but in your second line you say it's on a subpath called wsdl. Both cannot be true.

    So change your code to either:

    <wsdl>${basedir}/src/main/resources/wsdl/dataaccess-numberconversion.wsdl</wsdl>
    <wsdlLocation>classpath:wsdl/dataaccess-numberconversion.wsdl</wsdlLocation>
    

    or to:

    <wsdl>${basedir}/src/main/resources/dataaccess-numberconversion.wsdl</wsdl>
    <wsdlLocation>classpath:dataaccess-numberconversion.wsdl</wsdlLocation>
    

    What's after /src/main/resources/ and classpath: must match.

    Whatever variant you choose make sure that:

    • the WSDL file is in its proper place inside the /src/main/resources/ folder of your project (directly there or in wsdl subfolder).
    • that all your code also reflects the correct classpath path (on the root of the classpath or inside a subpackage called wsdl);
    • that after packaging your JAR file, you decompress it and look inside it to see the WSDL file is at the correct location inside the JAR (either directly on the root or in a a wsdl subfolder).