从SOAP wsdl生成客户端jar

pre*_*ret 9 java apache ant soap web-services

我正在尝试与一些具有基本身份验证的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

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

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

log*_*gan 12

马克的回答是有效的,但我更像是一个Maven的家伙,并希望最终对输出罐进行整理.

以下是如何使用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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*nor 7

Axis2支持多种支持Web服务客户端的方法.最常见的方法是记录在这里,涉及生成解析由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
Run Code Online (Sandbox Code Playgroud)

这将生成以下文件:

??? mydir
    ??? build.xml
    ??? src
        ??? com
            ??? xmlme
                ??? webservices
                    ??? ShakespeareStub.java
Run Code Online (Sandbox Code Playgroud)

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

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

cd mydir
ant
Run Code Online (Sandbox Code Playgroud)

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

??? mydir
    ??? build
    ?   ??? classes
    ?   ?   ??? ..
    ?   ?       ..
    ?   ??? lib
    ?       ??? Shakespeare-test-client.jar
    ??? build.xml
    ??? src
        ??? com
            ??? xmlme
                ??? webservices
                    ??? ShakespeareStub.java
Run Code Online (Sandbox Code Playgroud)

这个jar文件现在可以被希望访问webservice 的java(或者在另一个答案中看到我的示例groovy脚本)包含在内.