Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Apache AXIS2 Client Jar From a WSDL Using MAVEN

DZone's Guide to

Creating Apache AXIS2 Client Jar From a WSDL Using MAVEN

In this article, we will try to create a client jar from a WSDL file through which we can communicate with the exposed SOAP web services.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Prerequisites

Before starting, you will need Java and Maven installed in your system. You can refer here.

BackDrop

In this article, we will try to create a client jar from a WSDL file through which we can communicate with the exposed SOAP web services. The technology used to create the client jar will be Apache Axis2. The main reason I am writing this is because I faced some issues while trying to create a client for my own requirements. The motive behind creating the client jar is avoiding the boilerplate code to generate the SOAP message to a great extent. There are other ways to create clients as well, each having there own advantages and trade-offs.

Jar Creation Process

Create a simple maven project with a POM file.

Copy and paste the POM file below.

<?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>{{your group id}}</groupId>
    <artifactId>{{your artifact id}}</artifactId>
    <version>{{version of jar}}</version>
    <name>{{name of jar}}</name>
    <build>
        <plugins>
            <!-- Generates JAVA source files from the WSDL -->
            <plugin>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
                <version>1.7.7</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsdl2code</goal>
                        </goals>
                        <configuration>
                            <packageName>{{name of the package to be used}}</packageName>
                            <wsdlFile>{{path of WSDL file to be used}}</wsdlFile>
                            <databindingName>xmlbeans</databindingName>
                            <!--TODO: Update this file with new WSDL versions -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>src</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- this is used for inheritance merges -->
                        <phase>package</phase>
                        <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.apache.openejb</groupId>
                                    <artifactId>openejb-javaagent</artifactId>
                                    <version>3.0-beta-2</version>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>target/generated-sources/axis2/wsdl2code/resources</directory>
            </resource>
            <resource>
                <directory>target/generated-sources/xmlbeans/resources</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
            <version>1.7.7</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Modify the fields between {{ .. }} as per your requirement.

For example: 

groupId : com.ash.wsdl.java

artifactId: wsdl2java

version: 1

name: wsdl2java

packageName: com.ash.wsdl.java

wsdlFile: full path of your WSDL file.

After that, all you need to do is run mvn clean install from the root of the project.

The client jar will be automatically created and added to the local m2 repository.

Using the Jar to Communicate

Once the jar is successfully created, you can add its dependency in your application like this:

<dependency>
  <groupId> com.ash.wsdl.java</groupId>
  <artifactId>wsdl2java</artifactId>
  <version>1</version>
</dependency>

You will also need some external dependencies for the client to work.

<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.20</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.20</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-xmlbeans</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.woden</groupId>
<artifactId>woden-core</artifactId>
<version>1.0M10</version>
</dependency>
<dependency>
<groupId>org.apache.ws.xmlschema</groupId>
<artifactId>xmlschema-core</artifactId>
<version>2.2.1</version>
</dependency>

You are good to go now.

Now, let's see how we can call the web services through the jar.

ServiceStub ServiceStub= new ServiceStub();
Options options =ServiceStub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
options.setProperty(HTTPConstants.CHUNKED, "false");
options.setManageSession(true);

Your service stub is ready to be used now. Just create the desired SOAP message objects from the Factory and make WS calls.

I am sharing one example from my application on how to use the stub.

OrderDocument orderDocument = OrderDocument.Factory.newInstance();
Order newOrder = orderDocument.addNewOrder();
OrderInput orderInput = newOrder.addNewInput();

OrderInputHeader requestHeader = orderInput.addNewHeader();
requestHeader.setServiceSessionKey("ash");
requestHeader.setRequestID("ash111");
requestHeader.setTimeout(10);
requestHeader.setPageSize(0);
requestHeader.setWaitForResponse(true);
requestHeader.setPagingDirection(0);

OrderInputParameters inputParameters = orderInput.addNewParameters();
OrderNumberArray orderNumberArray = inputParameters.addNewOrderNumberArray1();
orderNumberArray.addOrderNumber(1);
orderNumberArray = inputParameters.addNewOrderNumberArray1();
orderNumberArray.addOrderNumber(2);
inputParameters.setRelatedOrders(false);


OrderResponseDocument orderResponseDocument = iosplusServiceStub.order(orderDocument);

All the elements extend XmlBeans, so a lot of useful functionalities are available by default. For example, just doing a toString on any of the objects will generate the corresponding SOAP message for that object.

Post your questions or concerns in the comments, and I will try to answer as quickly as I can.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
soap api ,axis2 ,maven ,java ,wsdl ,wsdl2java ,integration ,jar

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}