WSDL 2 Java Generate SOAP client from URL

SOAP stands for Simple Object Access Protocol; it is an XML ( eXtensible Markup Language) based protocol to exchange messages with a web service. SOAP uses HTTP for communication

To generate a Java SOAP client from a WSDL file, you can use the following steps:

Open a command prompt or terminal and navigate to the directory where you want to generate the client code.

Use the wsimport tool that comes with the Java Development Kit (JDK) to generate the client code. The wsimport tool reads the WSDL file and generates the client code. The syntax of the wsimport command is as

wsimport <WSDL_URL_OR_FILE_PATH> -d <DIRECTORY_TO_STORE_GENERATED_FILES>

Replace <WSDL_URL_OR_FILE_PATH> with the URL or file path to the WSDL file, and <DIRECTORY_TO_STORE_GENERATED_FILES> with the directory where you want to store the generated client code.

For example, if the WSDL file is located at http://asvignesh.in/MyService?wsdl and you want to store the generated code in a directory named generated, you can use the following

wsimport http://example.com/MyService?wsdl -d /Users/vignesh/MyService/generated

Wait for the wsimport tool to finish generating the client code. Once it’s finished, you should see several Java files and directories in the directory you specified in the previous step.

Use the generated client code in your Java application to call the SOAP web service.

or you can also use Maven to generate the code

To generate a Java SOAP client from a WSDL file using Maven, you can use the jaxws-maven-plugin plugin.

Here are the steps:

In your project directory, create a new directory to store the generated client code. For example, you could create a directory called generated-sources.

Add the jaxws-maven-plugin plugin to your project’s pom.xml file. Here’s an example configuration:xml

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <wsdlUrls>
                            <wsdlUrl>http://asvignesh.in/MyService?wsdl</wsdlUrl>
                        </wsdlUrls>
                     <packageName>in.asvignesh.myservice.client</packageName>
                        <outputDirectory>${project.build.directory}/generated-sources/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Replace http://asvignesh.in/MyService?wsdl with the URL or file path to the WSDL file you want to use, in.asvignesh.myservice.client with the name of the package you want to use for the generated client code, and ${project.build.directory}/generated-sources/ with the path to the directory where you want to store the generated client code.

Run the Maven generate-sources goal to generate the client code. You can run this command from the command line or from within your IDE.

mvn generate-sources

Use the generated client code in your Java application to call the SOAP web service. The package name and class names of the generated code will depend on the package name you specified in the jaxws-maven-plugin configuration.


Also published on Medium.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading