Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

How to parse WSDL using JAX-WS Maven plugin and generate java classes?


Java API for XML Web Services (JAX-WS) is a Java API for creating SOAP based web services, part of the Java EE platform. If you are using Maven as a build tool, you can use the JAX-WS Maven plugin to parse your WSDL file. In the maven pom.xml you have to mention all the details of this plugin under tag. There could be two different approaches as detailed below. - Article authored by Manika Paul Chowdhury on .

In the previous blog post, we learnt how to parse WSDL using the 'wsimport' tool. That's not the only option to parse a WSDL file in Java. If you are using Maven as a build tool, you can perform the same using 'JAX-WS' Maven plugin.

 

In this tutorial, we are going to learn how you can use the 'JAX-WS' Maven plugin to parse a WSDL file to generate java classes.

 

How to parse WSDL using JAX-WS Maven plugin and generate java classes?

 

Java API for XML Web Services (JAX-WS) is a Java API for creating SOAP based web services, part of the Java EE platform. If you are using Maven as a build tool, you can use the JAX-WS Maven plugin to parse your WSDL file. In the maven pom.xml you have to mention all the details of this plugin under <build> tag. There could be two different approaches as detailed below:

 

By setting the wsdlUrl tag

The first option is by setting the wsdlUrl tag of the plugin, which resides in the pom.xml file. To do this, open your pom.xml file and add the JAX-WS Maven plugin under the build | plugins tag. Then fill all the required details as shown in the below code snippet:

 

<build>
   <plugins>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>jaxws-maven-plugin</artifactId>
         <version>2.4.1</version>
         <executions>
            <execution>
               <id>wsdltoJava</id>
               <goals>
                  <goal>wsimport</goal>
               </goals>
               <configuration>
                  <wsdlUrls>
                     <wsdlUrl>[WSDL_URL]</wsdlUrl>
                  </wsdlUrls>
                  <vmArgs>
                     <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>  
                  </vmArgs>
                  <keep>true</keep>
                  <packageName>[TARGET_NAMESPACE]</packageName>
                  <sourceDestDir>[SOURCE_DIRECTORY]</sourceDestDir>
               </configuration>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>

 

* Make sure to set the [WSDL_URL], [TARGET_NAMESPACE] and [SOURCE_DIRECTORY], as highlighted above.

 

By setting the wsdlFile tag

The another option to generate the java classes is by setting the wsdlFile tag of the plugin, which resides in the pom.xml file. If you have the .wsdl file saved locally, you can go with this option. To do this, open your pom.xml file and add the JAX-WS Maven plugin under the build | plugins tag. Then fill all the required details as shown in the below code snippet:

 

<build>
   <plugins>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>jaxws-maven-plugin</artifactId>
         <version>2.4.1</version>
         <executions>
            <execution>
               <id>wsdltoJava</id>
               <goals>
                  <goal>wsimport</goal>
               </goals>
               <configuration>
                  <wsdlDirectory>[WSDL_FILE_PATH]</wsdlDirectory>
                  <wsdlFiles>
                     <wsdlFile>[WSDL_FILE_NAME]</wsdlFile>
                  </wsdlFiles>
                  <vmArgs>
                     <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>  
                  </vmArgs>
                  <keep>true</keep>
                  <packageName>[PACKAGE_NAME]</packageName>
                  <sourceDestDir>[SOURCE_DIRECTORY]</sourceDestDir>
               </configuration>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>

 

* Make sure to set the [WSDL_FILE_PATH], [WSDL_FILE_NAME], [TARGET_NAMESPACE] and [SOURCE_DIRECTORY], as highlighted above.

 

Points to note that:
   - The <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg> is required for JDK 8 or higher versions. If you don't specify this while parsing the WSDL, it will throw error.
   - The <keep> tag is used to keep the generated files. If you don't specify it, then it will be set as true by default.
   - The <sourceDestDir> is used to specify where to place the generated source files. If you don't specify it , the .java files will be generated by default inside the target/generated-sources/wsimport folder and the .class files will be generated in the target/classes folder.
   - The <packageName> is used to specify the target namespace. If you don't specify this tag, the generated .java and .class files will be placed under the same namespace which is mentioned in the WSDL.

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.