Eclipse Juno + Maven + CXF Generate WebService Client

Environment

Eclipse Juno

JDK 1.6

CXF 2.5.2

UTF-8, Formater

Eclipse 的 CXF Generate Client 不支援 JDK 6,一定要 JDK 7 以上才會成功

因此需使用 Maven 的 Plug-in 去 gen code,或command line 執行wsdl2java

Process

Step 1. Open Eclipse→Maven→Maven Project→Create a simple project→Group Id: [my.group], Artifact Id: [my-wsclient], Packaging: jar

Step 2. Edit pom.xml

dependency:
cxf-rt-frontend-jaxws
cxf-rt-transports-http

<build>
 <finalName>CxfClient</finalName>
 <plugins>
  <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/gen/java</sourceRoot>
      <wsdlOptions>
       <wsdlOption>
        <wsdl>${basedir}/src/main/resources/wsdl/MyWebService.wsdl</wsdl>
        <wsdlLocation>/wsdl/MyWebService.wsdl</wsdlLocation>
        <bindingFiles>
         <bindingFile>${basedir}/src/main/resources/wsdl/jaxws-bindings.xml</bindingFile>
        </bindingFiles>
       </wsdlOption>
      </wsdlOptions>
     </configuration>
     <goals>
      <goal>wsdl2java</goal>
     </goals>
    </execution>
   </executions>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.3.2</version>
   <configuration>
    <fork>true</fork>
    <verbose>true</verbose>
    <source>1.6</source>
    <target>1.6</target>
    <encoding>${project.build.sourceEncoding}</encoding>
   </configuration>
  </plugin>
 </plugins>
</build>

Step 3. Run As→Maven generate-sources

一般 gen code 會產生類似 JAXBElement<T> 這一類的物件,使用上很不方便。使用 jaxws-bindings.xml 設定generateElementProperty=”false“,才會直接產生T物件,例如 String 物件

<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
 xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <jaxb:bindings>
 <jaxb:globalBindings generateElementProperty="false" />
 </jaxb:bindings>
</jaxb:bindings>

Reference

http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html

https://docs.oracle.com/cd/E19159-01/820-1072/ahiid/index.html

http://stackoverflow.com/questions/4455195/how-to-avoid-the-need-to-specify-the-wsdl-location-in-a-cxf-or-jax-ws-generated/9875701#9875701

https://docs.oracle.com/cd/E21764_01/web.1111/e13734/clients.htm#WSADV584

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s