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.
- Download
Apache Axis2 from here.
- Extract
the downloaded file to a location on your machine.
- 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:
- Compile
your Java code, ensuring that the Axis2 libraries are in your classpath.
- 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
- Download
Apache Axis2 from the official website:
- Extract
the Axis2 archive file to a folder on your machine.
- 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:
- Go to
Help > Eclipse Marketplace in Eclipse.
- Search
for "Web Tools Platform" or "WTP."
- Install
the "Eclipse Web Tools" plugin if it isn't already installed.
Step 3: Configure Axis2 in Eclipse
- Open
Eclipse and go to Window > Preferences.
- Expand
the Web Services section and select Axis2 Preferences.
- 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).
- Click
Apply and Close.
Step 4: Create a New Java Project
- Go
to File > New > Java Project.
- Name
your project (e.g., Axis2Wsdl2JavaProject).
- Click
Finish.
Step 5: Add Axis2 Libraries to the Project
- Right-click
your project in the Project Explorer.
- Select
Build Path > Configure Build Path.
- In
the Libraries tab, click Add External JARs.
- Navigate
to the lib folder inside your Axis2 installation and select all the JAR
files.
- 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.
- Right-click
your project in the Project Explorer.
- Select
New > Other.
- In
the dialog box, expand Web Services and choose Axis2 Code
Generator > Axis2 Code Generator.
- Click
Next.
- 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).
- Click
Next.
Step 7: Configure Generation Options
- Choose
the Data Binding option, which can be one of the following:
ü
ADB: Axis2 Data Binding (default).
ü
XMLBeans or JAXB if required.
- Select
whether you want to generate client-side or server-side
code.
- 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.
- Right-click
your project, choose Build Path > Configure Build Path.
- Add
the Axis2 JARs under Add External JARs if not already added.
- 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.
0 Comments