【问题标题】:generating client jar from SOAP wsdl从 SOAP wsdl 生成客户端 jar
【发布时间】:2013-08-20 19:05:03
【问题描述】:

我正在尝试与一些具有基本身份验证的 SOAP Web 服务进行交互,并且我有 url、用户名和密码。现在我想在我的 java 代码中使用这个 web 服务,所以我需要为它创建一个 jar 文件。

我看过以下网址,但不确定我是否正确地遵循了它。 http://axis.apache.org/axis2/java/core/docs/userguide-creatingclients.html#choosingclient http://javasourcecodeetc.blogspot.com/2011/07/convert-wsdl-to-java-for-calling-soap.html

我已从以下位置下载轴 2-1.6.2 http://axis.apache.org/axis2/java/core/download.cgi(仅二进制分发)

我有给定的客户端存根...我看到有人说要与 build.xml 一起使用,但我在任何地方都找不到 build.xml ....请告诉我我需要安装什么设置 apache 轴和蚂蚁?蚂蚁在这里做什么?

【问题讨论】:

    标签: java web-services apache soap ant


    【解决方案1】:

    Mark 的回答有效,但我更像是一个 Maven 人,并希望最终对输出 jar 进行 mavenize。

    以下是使用 Maven 的方法。

    1. 将 WSDL 下载到目录(例如 mydir/MyWsdl.wsdl)。
    2. 创建pom.xml文件(如下图)。
    3. 运行mvn package

    这就是你最终会得到的结果

    └── mydir
        ├── MyWsdl.wsdl
        ├── pom.xml
        └── target (add this dir to .gitignore)
            ├── generated-sources
            ├── mywsdl-0.1.0-SNAPSHOT.jar
            ├── mywsdl-0.1.0-SNAPSHOT-sources.jar
            └── mywsdl-0.1.0-SNAPSHOT-javadoc.jar
    

    以及pom.xml文件的来源

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>mywsdl</artifactId>
      <version>0.1.0-SNAPSHOT</version>
      <name>My WSDL client</name>
      <build>
        <plugins>
          <!-- Generates JAVA source files from the WSDL -->
          <plugin>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
            <version>1.6.2</version>
            <executions>
              <execution>
                <goals>
                  <goal>wsdl2code</goal>
                </goals>
                <configuration>
                  <packageName>com.example</packageName>
                  <wsdlFile>MyWsdl.wsdl</wsdlFile>
                  <!-- TODO: Update this file with new WSDL versions -->
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      <dependencies>
        <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2</artifactId>
          <version>1.6.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-adb</artifactId>
          <version>1.6.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          <artifactId>axiom-api</artifactId>
          <version>1.2.14</version>
        </dependency>
        <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          <artifactId>axiom-impl</artifactId>
          <version>1.2.14</version>
        </dependency>
      </dependencies>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    </project>
    

    【讨论】:

    • 我无法编辑,因为它会少于 6 个字符,但是 pom.xml 的第一行中缺少 x:“x”mlns:xsi="w3。 ....
    【解决方案2】:

    Axis2 支持多种方式来支持 Web 服务客户端。最常见的方法记录在 here 并涉及生成解析 WSDL 文件描述的 SOAP 消息的 Java 代码。

    以下答案描述了调用 Web 服务的多种方法。最后一部分描述了一个 groovy 脚本,它使用 Axis2 生成的类并使用 ANT 编译:

    更多细节

    wsdl2java 程序(与 Axis2 捆绑)将根据指定的 WSDL 文件生成一个 ANT 项目:

    $AXIS2_HOME/bin/wsdl2java.sh -d adb -s -o mydir -uri http://www.xmlme.com/WSShakespeare.asmx?WSDL
    

    这将生成以下文件:

    └── mydir
        ├── build.xml
        └── src
            └── com
                └── xmlme
                    └── webservices
                        └── ShakespeareStub.java
    

    如果您检查生成的 java 代码,您会发现与 WSDL 文件中定义的 XML 模式类型相匹配的 java 类,从而更简单地序列化和反序列化 SOAP 消息。

    “build.xml”文件包含编译生成的 java 代码的逻辑。

    cd mydir
    ant
    

    当构建运行时,它会默认创建如下的 jar 文件:

    └── mydir
        ├── build
        │   ├── classes
        │   │   └── ..
        │   │       ..
        │   └── lib
        │       └── Shakespeare-test-client.jar
        ├── build.xml
        └── src
            └── com
                └── xmlme
                    └── webservices
                        └── ShakespeareStub.java
    

    这个 jar 文件现在可以包含在希望访问 web 服务的 java(或查看我在 other answer 中的示例 groovy 脚本)中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多