Java 从 SOAP wsdl 生成客户端 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18306097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:19:51  来源:igfitidea点击:

generating client jar from SOAP wsdl

javaweb-servicesapachesoapant

提问by pret

i am trying to interact with some SOAP web service which has the basic authentication and I have the url, username and password. Now I want to use this web service in my java code and so i need to create a jar file for it.

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

i have seen the below URLs but not sure if I followed it correctly. http://axis.apache.org/axis2/java/core/docs/userguide-creatingclients.html#choosingclienthttp://javasourcecodeetc.blogspot.com/2011/07/convert-wsdl-to-java-for-calling-soap.html

我看过以下网址,但不确定我是否正确地遵循了它。 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

I have downloaded axis 2-1.6.2 from http://axis.apache.org/axis2/java/core/download.cgi(only binary distribution)

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

I have the client stub which was given ... I see people saying to use it with build.xml, but i couldn't find build.xml anywhere .... Please tell me what all i need to install to set up apache axis and ant ? What is ant doing here?

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

回答by Mark O'Connor

Axis2 supports several ways to support Web service clients. The most common approach is documented hereand involved generating Java code that parse the SOAP message described by the WSDL file.

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

The following answer describes a number of ways to invoke a web service. The last part describes a groovy script that uses the classes generated by Axis2 and compiled using ANT:

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

More detail

更多详情

The wsdl2java program (bundled with Axis2) will generate an ANT project based on the specified WSDL file:

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

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

This will generate the following files:

这将生成以下文件:

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

If you check the generated java code you'll discover java classes that match the XML schema types defined in the WSDL file, making it simpler to serialize and deserialize SOAP messages.

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

The "build.xml" file contains the logic that will compile the generated java code.

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

cd mydir
ant

When the build runs it will by default create jar file as follows:

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

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

This jar file can now be included by a java (or see my example groovy script in the other answer) that wishes to access the webservice.

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

回答by logan

Mark's answer works, but I'm more of a Maven guy and want to eventually mavenize the output jar.

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

Here's how to do it with Maven.

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

  1. Download a WSDL to a directory (e.g. mydir/MyWsdl.wsdl).
  2. Create a pom.xmlfile (as shown below).
  3. Run mvn package.
  1. 将 WSDL 下载到目录(例如mydir/MyWsdl.wsdl)。
  2. 创建一个pom.xml文件(如下所示)。
  3. 运行mvn package

Here's what you'll end up with

这就是你最终会得到的

└── 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

And the source of the pom.xmlfile

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>