Generate Java classes from WSDL command line

Grace 82 Published: 11/06/2024

Generate Java classes from WSDL command line

I'll respond in English as per your request.

To generate Java classes from a WSDL file using the command line, you can use the wsdl2java tool provided by Apache CXF. Here's how to do it:

Prerequisites:

Install Apache CXF on your machine. Download the WSDL file (let's assume it's named mywsdl.wsdl) and place it in a directory of your choice.

Generating Java classes from WSDL:

Open a command prompt or terminal window. Navigate to the directory where you placed the WSDL file. Run the following command:
wsdl2java -client jaxws21 -d mypackage -p com.example.myproject http://myserver.com/mywsdl.wsdl

Here's a brief explanation of the options used:

-client jaxws21: Specifies that you want to use JAX-WS 2.1 as the client API. -d mypackage: Generates Java code in a package named mypackage. -p com.example.myproject: Specifies the package name for the generated Java classes (in this case, com.example.myproject). http://myserver.com/mywsdl.wsdl: Provides the URL of the WSDL file to generate code from.

What's generated:

The wsdl2java tool will generate a set of Java classes that correspond to the services and operations defined in the WSDL file. These classes are:

A service class (e.g., MyService.java) that represents the web service. Multiple operation classes (e.g., MyOperation1.java, MyOperation2.java, etc.) that represent the individual operations defined in the WSDL file. Helper classes (e.g., MyPortType.java) that provide utility methods for working with the WSDL file.

Additional steps:

After generating the Java code, you'll need to compile and package it using a tool like Apache Maven or Gradle. You may also need to create a deployment descriptor (e.g., web.xml in a WAR file) to configure your web service.

That's it! With these simple steps, you've successfully generated Java classes from a WSDL file using the command line.

How to read data from WSDL File in Java?

I'll make sure to respond in English for this question.

Reading data from a Web Services Description Language (WSDL) file in Java involves using the JAX-WS (Java API for XML-Based Web Services) implementation provided by Apache CXF or Sun's Metro. Here's an example of how you can achieve this:

Using Apache CXF

Add the Apache CXF dependencies to your project: cxf-api and cxf-rt-frontend-jaxws. Create a class that reads the WSDL file:
import org.apache.cxf.wsdl11.WSDLException;

import org.apache.cxf.wsdl11.WSDLReader;

public class WsdlReader {

public static void main(String[] args) {

try {

// Load the WSDL file

WSDLReader reader = new WSDLReader();

URL wsdlUrl = new URL("http://example.com/service?wsdl");

Definition definition = reader.read(wsdlUrl);

// Get the port and operations

QName portQName = definition.getPort(0).getPortType().getName();

Service service = definition.getService();

List operations = service.getInterface(portQName).getOperations();

// Print the operation names

for (Operation operation : operations) {

System.out.println(operation.getName());

}

} catch (WSDLException | MalformedURLException e) {

e.printStackTrace();

}

}

}

In this example, we load the WSDL file using the WSDLReader class and then extract the port, operations, and their names.

Using Sun's Metro

Add the Sun's Metro dependencies to your project: metro-xml-jaxws-api and jaxws-api. Create a class that reads the WSDL file:
import javax.xml.ws.Endpoint;

import javax.xml.ws.WebServiceDescription;

public class WsdlReader {

public static void main(String[] args) {

try {

// Load the WSDL file

Endpoint endpoint = new Endpoint("http://example.com/service", null);

WebServiceDescription wsdlDoc = endpoint.getWsdlDocument();

// Get the port and operations

List webServices = wsdlDoc.getPortList();

for (WebService webService : webServices) {

List operations = webService.getOperationList();

for (Operation operation : operations) {

System.out.println(operation.getName());

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

In this example, we create an Endpoint instance and then retrieve the WSDL document using the getWsdlDocument() method. We can then extract the port and operations from the WSDL document.

Additional Steps

After reading the WSDL file, you may also want to perform additional steps such as:

Creating a client class that uses the Java API for XML-Based Web Services (JAX-WS) to invoke the web service. Handling faults and exceptions when calling the web service. Implementing authentication and authorization mechanisms to secure the web service.

Remember to replace http://example.com/service?wsdl with your actual WSDL file URL.