无法从 classpath:wsdl 初始化默认 wsdl

Can not initialize the default wsdl from classpath:wsdl

我通过一个示例来了解 SOAP 的工作原理。我已经使用 Apache cxf 从 wsdl 生成了代码,我可以记录 SOAP Web 服务请求和响应。显然一切正常。我在设置相对路径时遇到问题。我已经按照这个解决方案 How to avoid the need to specify the WSDL location in a CXF or JAX-WS generated webservice client?,但是没有办法解决控制台上的日志错误。

控制台上的错误信息:

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

这是我的 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>

我的服务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);
    }

}

当我 运行 这个代码 URL url = NumberConversion.class.getClassLoader().getResource("wsdl/dataaccess-numberconversion.wsdl"); , 我会得到一个空值。是 NumberConversion.class.getClassLoader() 调用的错误路径,因为这个 class 在另一个包中。

知道如何以正确的方式设置吗?

您的 <wsdlOption><wsdl><wsdlLocation> 值不匹配。

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

第一行说该文件位于类路径的根目录中,但在第二行中您说它位于名为 wsdl 的子路径中。两者不可能都是真的。

所以将您的代码更改为:

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

或:

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

/src/main/resources/classpath:后面的必须匹配。

无论您选择哪种变体,请确保:

  • WSDL 文件位于您项目的 /src/main/resources/ 文件夹中的适当位置(直接位于该文件夹或 wsdl 子文件夹中)。
  • 您的所有代码也反映了正确的类路径路径(在类路径的根目录或名为 wsdl 的子包内);
  • 在打包您的 JAR 文件后,您将其解压缩并查看其中的内容,以查看 WSDL 文件是否位于 JAR 中的正确位置(直接位于根目录或 wsdl 子文件夹中)。