Generate a Web Service (JAX-WS) Client with Maven

April 3, 2012

Java EE, Tooling

Generating a web service (JAX-WS) client with Maven is a two step process. The first step is generate a WSDL document from the JAX-WS annotated class. The next step is to generate the Java client from the generated WSDL document.

The following Maven plugins are required:

  • cxf-java2ws-plugin (JAX-WS to WSDL)
  • cxf-codegen-plugin (WSDL to Java)
JAX-WS to WSDL

The first step is to generate the WSDL document from the JAX-WS annotated class by configuring cxf-java2ws-plugin with the ‘java2ws’ goal.

Add the cxf-rt-frontend-jaxws dependency and project dependencies required for the JAX-WS annotated class as plugin dependencies, the name of the JAX-WS annotated class, and configure the plugin to generate a WSDL.

The generated WSDL will be placed in the target/generated/wsdl directory.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-java2ws-plugin</artifactId>
   <version>2.5.1</version>
   <dependencies>
      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>2.5.1</version>
      </dependency>
      <dependency>
         <groupId>com.company.app</groupId>
         <artifactId>services</artifactId>
         <version>0.0.1-SNAPSHOT</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <className>com.company.app.services.WebServiceBean</className>
            <genWsdl>true</genWsdl>
         </configuration>
         <goals>
            <goal>java2ws</goal>
         </goals>
      </execution>
   </executions>
</plugin>
WSDL to Java

The next step is to generate the Java client from the generated WSDL document by configuring cxf-codegen-plugin with the ‘wsdl2java’ goal.

The ‘-p’ argument specifies the package for the generated classes. The reason for configuring this argument is to generate a unique package for the client model and ensure that there is no conflict between the client and service models.

The generated classes will be placed in the target/generated-sources/cxf folder.

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>2.5.1</version>
   <executions>
      <execution>
         <id>process-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${project.build.directory}/generated/wsdl/WebServiceBean.wsdl</wsdl>
                  <extraargs>
                     <extraarg>-p</extraarg>
                     <extraarg>com.company.app.services.client.model</extraarg>
                  </extraargs>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>
, , ,

About Shane K Johnson

Technical Marketing Manager, Red Hat Inc.

View all posts by Shane K Johnson

3 Comments on “Generate a Web Service (JAX-WS) Client with Maven”

  1. filosofisto Says:

    What Maven linecommand must use to create stubs?

    Reply

  2. bhandari4u Says:

    wsimport allows to generate data type classes. Eclipse and ant both have got support for running wsimport task. Earlier wsimport was called WSDL to Java.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: