Header Ads Widget

Responsive Advertisement

Wsdl2Java Convert using Axis2


WSDL2Java is a tool provided by Apache Axis2 that helps generate Java classes from a WSDL (Web Services Description Language) file. These classes can be used to interact with web services in a Java-based client or server environment.

Steps to Convert WSDL to Java using Apache Axis2

1. Install Apache Axis2

First, you need to download and install Apache Axis2.

  1. Download Apache Axis2 from here.
  2. Extract the downloaded file to a location on your machine.
  3. Set up the AXIS2_HOME environment variable to point to the folder where you extracted Axis2.

2. Set Up Axis2 Environment

  • Make sure the axis2 and wsdl2java commands are in your path:

bash

export AXIS2_HOME=<path_to_axis2>

export PATH=$AXIS2_HOME/bin:$PATH

 

3. Using WSDL2Java Tool

The WSDL2Java tool provided by Axis2 can be used from the command line to generate Java classes from the WSDL file.

  • Syntax:

bash

wsdl2java -uri <path_to_wsdl_file> -p <package_name> -d adb

 

Explanation of Options:

Ø  -uri <path_to_wsdl_file>: Path to the WSDL file.

Ø  -p <package_name>: The package name for the generated Java classes.

Ø  -d <databinding_method>: The data binding framework to use (default is adb).

ü  adb: Axis2 Data Binding (most common)

ü  xmlbeans: XMLBeans-based data binding

ü  jaxbri: JAXB RI-based data binding

Example Command:

bash

wsdl2java -uri https://example.com/service?wsdl -p com.example.client -d adb

 

4. Review Generated Java Code

The WSDL2Java tool will generate several Java classes in the specified package. These classes will include:

Ø  Stubs: To interact with the web service.

Ø  Data classes: Representing the complex types from the WSDL.

Ø  Exceptions: Representing any faults defined in the WSDL.

5. Add Required Axis2 Libraries

You will need to include the Axis2 libraries (found in the lib directory of Axis2) in your project classpath for the generated code to work.

6. Invoke Web Services in Java

After generating the Java classes, you can write client code to invoke the web service methods.

Here’s a basic example of invoking a method from the generated stub:

Java

package com.example.client;

 

import com.example.service.ServiceStub;

 

public class ServiceClient {

    public static void main(String[] args) {

        try {

            // Initialize the stub

            ServiceStub stub = new ServiceStub();

 

            // Prepare the request

            ServiceStub.MyRequest request = new ServiceStub.MyRequest();

            request.setParam1("SomeValue");

 

            // Invoke the service method

            ServiceStub.MyResponse response = stub.myServiceMethod(request);

 

            // Print the response

            System.out.println("Response: " + response.getReturnValue());

 

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

7. Build and Run the Client

To run your client:

  1. Compile your Java code, ensuring that the Axis2 libraries are in your classpath.
  2. Execute the main method in your client to interact with the web service.

Optional Flags in wsdl2java:

Ø  -s: Generate server-side code.

Ø  -o: Specify output directory for the generated code.

Ø  -uw: Unwrap the generated classes to remove additional layers of method calls.

Ø  -ns2p: Map namespaces to package names (useful when working with multiple namespaces).


To convert a WSDL file into Java classes using Axis2 in Eclipse, follow these steps:

1. Install Apache Axis2 in Eclipse

First, you need to have Axis2 integrated into Eclipse. Eclipse has built-in support for web service tools, but you might need to add Axis2 libraries manually for WSDL2Java conversion.

Step-by-Step Guide:

Step 1: Download Axis2 Libraries

  1. Download Apache Axis2 from the official website:

ü  Download Axis2

  1. Extract the Axis2 archive file to a folder on your machine.
  2. Note the path to the Axis2 libraries (inside the lib folder of Axis2).

Step 2: Install Eclipse Web Tools Platform (WTP)

Ensure that the Eclipse Web Tools Platform (WTP) is installed. This is required for web service-related development.

To install:

  1. Go to Help > Eclipse Marketplace in Eclipse.
  2. Search for "Web Tools Platform" or "WTP."
  3. Install the "Eclipse Web Tools" plugin if it isn't already installed.

Step 3: Configure Axis2 in Eclipse

  1. Open Eclipse and go to Window > Preferences.
  2. Expand the Web Services section and select Axis2 Preferences.
  3. In the Axis2 Runtime Location, click Browse and navigate to the folder where you extracted the Axis2 distribution (for example, C:/axis2-1.7.9).
  4. Click Apply and Close.

Step 4: Create a New Java Project

  1. Go to File > New > Java Project.
  2. Name your project (e.g., Axis2Wsdl2JavaProject).
  3. Click Finish.

Step 5: Add Axis2 Libraries to the Project

  1. Right-click your project in the Project Explorer.
  2. Select Build Path > Configure Build Path.
  3. In the Libraries tab, click Add External JARs.
  4. Navigate to the lib folder inside your Axis2 installation and select all the JAR files.
  5. Click Open, then Apply and Close.

Step 6: Generate Java Classes from WSDL (WSDL2Java)

Now, use Eclipse's Axis2 wizard to generate Java classes from the WSDL.

  1. Right-click your project in the Project Explorer.
  2. Select New > Other.
  3. In the dialog box, expand Web Services and choose Axis2 Code Generator > Axis2 Code Generator.
  4. Click Next.
  5. In the Axis2 Code Generator Wizard:

ü  Select the Generate Java source code from a WSDL file option.

ü  In the WSDL File Location, browse to the location of your WSDL file.

ü  Set the Output Location (where the generated Java code will be placed).

ü  Specify a Target Package (e.g., com.example.client).

  1. Click Next.

Step 7: Configure Generation Options

  1. Choose the Data Binding option, which can be one of the following:

ü  ADB: Axis2 Data Binding (default).

ü  XMLBeans or JAXB if required.

  1. Select whether you want to generate client-side or server-side code.
  2. Click Finish.

Step 8: Review Generated Code

Once the wizard completes, Eclipse will generate the required Java classes (stubs, data classes, etc.) based on your WSDL.

You should now see the generated Java classes in your project under the specified package name. These classes will include methods for interacting with the web service.

Step 9: Create and Run a Client

Now, you can write a client class to invoke the web service using the generated stubs. For example:

java

package com.example.client;

 

import com.example.service.ServiceStub;

 

public class ServiceClient {

    public static void main(String[] args) {

        try {

            // Initialize the stub

            ServiceStub stub = new ServiceStub();

 

            // Prepare the request

            ServiceStub.MyRequest request = new ServiceStub.MyRequest();

            request.setParam1("SomeValue");

 

            // Invoke the service method

            ServiceStub.MyResponse response = stub.myServiceMethod(request);

 

            // Print the response

            System.out.println("Response: " + response.getReturnValue());

 

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

 

Step 10: Add Axis2 Libraries to the Client Classpath

Make sure all Axis2-related JARs are in your classpath for the client to run successfully.

  1. Right-click your project, choose Build Path > Configure Build Path.
  2. Add the Axis2 JARs under Add External JARs if not already added.
  3. Click Apply and Close.

Step 11: Run the Client

Ø  Right-click the client class in Project Explorer.

Ø  Choose Run As > Java Application.

Ø  The client will make a web service request and print the response in the console.


This process allows you to convert a WSDL file to Java classes and integrate them into your Java project using Eclipse and Apache Axis2.

 


Env Setup
Env Setup




Display Env Setup
Display Env Setup

 

Step -Step
?


Apache Axis setup 1
Apache Axis setup 1









Create A wsdl file after :
?
1>Start your web project
2>Create a wsdl file and right click on WSDL file
Apache Axis create wsdl file
Apache Axis create wsdl file





Generated file compile and run
Generated file compile and run

Post a Comment

0 Comments