关于Java:从SOAP wsdl生成客户端jar

generating client jar from SOAP wsdl

我正在尝试与一些具有基本身份验证的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(仅二进制分发版)

我有给定的客户Stubbing...我看到有人说要在build.xml中使用它,但是我在任何地方都找不到build.xml...。请告诉我我需要安装什么设置apache轴和ant?ant在这里做什么?


Mark的答案有效,但我更像是一个Maven家伙,想最终实现输出jar的美化。

这是使用Maven进行操作的方法。

  • 将WSDL下载到目录(例如mydir/MyWsdl.wsdl)。
  • 创建一个pom.xml文件(如下所示)。
  • 运行mvn package
  • 这就是你要得到的结果

    1
    2
    3
    4
    5
    6
    7
    8
    └── 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文件的源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    <?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>
      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>
            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>
            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>
            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>
          axis2</artifactId>
          <version>1.6.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.axis2</groupId>
          axis2-adb</artifactId>
          <version>1.6.2</version>
        </dependency>
        <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          axiom-api</artifactId>
          <version>1.2.14</version>
        </dependency>
        <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          axiom-impl</artifactId>
          <version>1.2.14</version>
        </dependency>
      </dependencies>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    </project>


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

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

    编译的类

    • 使用Axis2创建Web服务的步骤-客户端代码

    更多详情

    wsdl2java程序(与Axis2捆绑在一起)将基于指定的WSDL文件生成ANT项目:

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

    这将生成以下文件:

    1
    2
    3
    4
    5
    6
    7
    └── mydir
        ├── build.xml
        └── src
            └── com
                └── xmlme
                    └── webservices
                        └── ShakespeareStub.java

    如果您检查生成的Java代码,则会发现与WSDL文件中定义的XML模式类型匹配的Java类,这使得序列化和反序列化SOAP消息更加容易。

    " build.xml"文件包含将编译生成的Java代码的逻辑。

    1
    2
    cd mydir
    ant

    构建运行时,默认情况下将创建jar文件,如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    └── mydir
        ├── build
        │ ├── classes
        │ │ └── ..
        │ │     ..
        │ └── lib
        │     └── Shakespeare-test-client.jar
        ├── build.xml
        └── src
            └── com
                └── xmlme
                    └── webservices
                        └── ShakespeareStub.java

    该jar文件现在可以由希望访问Web服务的Java包含(或在其他答案中查看我的示例groovy脚本)。