7/30/2013

WSDL Client Stub Generation (JAVA, Maven, Apache CXF)

To consume WSDL-based web service. The easiest way to do is to generate client stub to make the service call.

Apache CXF is one service framework for web services. Details could be found in official web-site:
http://cxf.apache.org/docs/overview.html

We use maven to manage client stub code, to integrate Apache CXF, we can add dependency:
<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <cxf.version>2.7.5</cxf.version>
</properties>

<dependencies>
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
 </dependency>
 <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>apache-cxf</artifactId>
   <version>${cxf.version}</version>
   <type>pom</type>
 </dependency>
</dependencies>
To generate client stub, we can use the 'cxf-codegen-plugin':
<plugin>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-codegen-plugin</artifactId>
 <version>${cxf.version}</version>
 <executions>
   <execution>
     <id>generate-sources</id>
     <phase>generate-sources</phase>
     <configuration>
       <sourceRoot>${basedir}/src/main/code</sourceRoot>
       <wsdlOptions>
         <wsdlOption>
           <wsdl>WSDL_URL/RELATIVE_PATH(e.g src/main/resources/wsdl/XXX.wsdl)</wsdl>
           <extraargs>
             <extraarg>-impl</extraarg>
             <extraarg>-verbose</extraarg>
             <extraarg>-client</extraarg>
             <extraarg>-p</extraarg>
             <extraarg>com.test.service.message</extraarg>
             <!-- <extraarg>-p</extraarg> <extraarg>SOME_NAMING_SPACE1=com.test.service.message.space1</extraarg>
          <extraarg>-p</extraarg> <extraarg>SOME_NAMING_SPACE2=com.test.service.message.space2</extraarg> 
          -->
           </extraargs>
         </wsdlOption>
         <wsdlOption>
           <wsdl>WSDL_URL/RELATIVE_PATH(e.g src/main/resources/wsdl/XXX2.wsdl)</wsdl>
           <extraargs>
             <extraarg>-impl</extraarg>
             <extraarg>-verbose</extraarg>
             <extraarg>-client</extraarg>
             <extraarg>-p</extraarg>
             <extraarg>com.test.service.directory</extraarg>
           </extraargs>
         </wsdlOption>
       </wsdlOptions>
     </configuration>
     <goals>
       <goal>wsdl2java</goal>
     </goals>
   </execution>
 </executions>
</plugin>

No comments:

Post a Comment