DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Private Remote Maven Repository With Artipie
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI

Trending

  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Product-Led Software Delivery: Intelligent Platforms for DevOps at Scale
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Creating Apache AXIS2 Client Jar From a WSDL Using MAVEN

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.

By 
Ashish Lohia user avatar
Ashish Lohia
·
Jun. 04, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
43.4K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JAR (file format) Apache Axis2 Apache Maven

Opinions expressed by DZone contributors are their own.

Related

  • Private Remote Maven Repository With Artipie
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Tracking Dependencies Beyond the Build Stage
  • Automating Maven Dependency Upgrades Using AI

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook