Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating client jar from SOAP wsdl

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.

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#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)

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?

like image 606
pret Avatar asked Aug 19 '13 03:08

pret


People also ask

How do I create a JAR file from WSDL?

Use wsimport command to generate Client side artifacts first. b. And then use jar utility command to create JAR file which shall contains all java classes. In case you want to automate these 2 steps, you can use ANT build tool and configure it's build.


2 Answers

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>
like image 151
logan Avatar answered Sep 19 '22 11:09

logan


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:

  • Steps in creating a web service using Axis2 - The client code

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.

like image 25
Mark O'Connor Avatar answered Sep 20 '22 11:09

Mark O'Connor