generating client jar from SOAP wsdl

41,077

Solution 1

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

Here's how to do it with Maven.

  1. Download a WSDL to a directory (e.g. mydir/MyWsdl.wsdl).
  2. Create a pom.xml file (as shown below).
  3. Run 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.xml file

<?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>

Solution 2

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

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:

More detail

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

$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.

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

cd mydir
ant

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

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

Share:
41,077
pret
Author by

pret

Updated on July 03, 2020

Comments