kunal-chowdhury.com

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


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.

 

 


If you have come this far, it means that you liked what you are reading (How to parse WSDL using JAX-WS Maven plugin and generate java classes?).

Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions.