无法从 classpath:wsdl 初始化默认 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>

我的服务类:

@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,QName serviceName,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,features);
    }

}

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

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

解决方法

您的 <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 内的正确位置(直接位于根目录或 aa wsdl 子文件夹中)。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...