快速上手使用CXF

一.  簡介

Apache CXF 是一個Service框架,他簡化了Service的創建, CXF實現了JAX-WS2.0規範,並通過了JAX-WS2.0 TCK; CXF和Spring無縫集成;CXF支持多種傳輸協議(HTTP, JMS, Corba等), 支持多種Binding數據格式(SOAP,XML,JSON等), 支持多種DataBinding數據類型(JAXB, Aegis) 。CXF基於Interceptor的架構,使得整個框架非常易於擴展。

二.  如何發佈並調用簡單的web service實例

2.1.下載:apache-cxf-2.1.1  http://cxf.apache.org/download.html

2.2. 新建java project ,並加入apache-cxf-2.0.7/lib所有包,編寫要發佈的web service 接口和實現

import javax.jws.WebService;

@WebService 

public interface HelloWorld {  

     public String sayHello(String text);  

}

import javax.jws.WebService;  

@WebService(endpointInterface="test.HelloWorld")  

public class HelloWorldImpl implements HelloWorld {  

      public String sayHello(String text) {  

                  return "Hello" + text ;  

    }  

  } 

@WebService 註解表示是要發佈的web 服務

name:用於Interface,屬映射到wsdl:portType element的name屬性。
targetNamespace:用於Interface和implement,如果不指定,缺省會使用包名倒序做爲wsdl名空間。

serviceName:用於implement,表示wsdl服務名。

portName:用於implement,表示wsdl:port 的name屬性。

endpointInterface:用於implement,指定Interface全名,包括包名。

2.3.發佈web service

public class Server {

    protected Server() throws Exception {

         System.out.println("Starting Server");

         HelloWorldImpl implementor = new HelloWorldImpl();

         String address = "http://localhost:9000/helloWorld";

         Endpoint.publish(address, implementor);

    }

    public static void main(String args[]) throws Exception {

        new Server();

        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);

        System.out.println("Server exiting");

        System.exit(0);

    }

}

運行後,在瀏覽器中輸入http://localhost:9000/helloWorld?wsdl將顯示這個web service的wsdl.說明web service發佈成功。

2.4.下面就開始創建一個客戶端程序,訪問這個web service, 同樣新建java project ,並加入apache-cxf-2.0.7/lib所有包,由於CXF已經提供wsdl轉化成java 的命令工具,所以創建一個build.xml,用來生成客戶端程序。Bulid.xml內容如下:

<?xml version="1.0"?>

<project name="cxf wsdl2java" basedir=".">  

   <property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>

   <path id="cxf.classpath">

      <fileset dir="${cxf.home}/lib">

         <include name="*.jar"/>

      </fileset>

   </path>     

   <target name="cxfWSDLToJava">

      <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">

         <arg value="-client"/>

         <arg value="-d"/>

         <arg value="src"/>

         <arg value="http://localhost:9000/helloWorld?wsdl"/>

         <classpath>

            <path refid="cxf.classpath"/>

         </classpath>

      </java>

   </target>

</project>

或者:配置環境變量%CXF_HOME%=E:/WebService/CXF/apache-cxf-2.1.1/apache-cxf-2.1.1(以我的目錄爲例),並在PATH後加上;%CXF_HOME%/bin

在cmd命令行中輸入wsdl2java如果顯示其用法表示配置好了。

輸入:wsdl2java -d src - client http://localhost:9000/helloWorld?wsdl

其作用上面的build.xml作用一樣。

附加:wsdl2java用法:

wsdl2java -p com -d src -all  aa.wsdl

-p  指定其wsdl的命名空間,也就是要生成代碼的包名:

-d  指定要產生代碼所在目錄

-client 生成客戶端測試web service的代碼

-server 生成服務器啓動web  service的代碼

-impl 生成web service的實現代碼

-ant  生成build.xml文件

-all 生成所有開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.

詳細用法見:http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html

2.5.調用web service

public class MyClient {

      public static void main(String[] argv) {

        HelloWorld  hello = new HelloWorldImplService().getHelloWorldImplPort();

        System.out.println(hello.sayHello("Tom") ); 

      }

    }

注意:運行時,要一定先要發佈web sevice.

三.  參考資料

1.CXF 主頁: http://cxf.apache.org/

2. CXF中文討論組: http://groups.google.com/group/cxf-zh 

3. Web service: http://www.w3school.com.cn/webservices/index.asp
4. WSDL: http://www.w3school.com.cn/wsdl/index.asp
5. SOAP:http://www.w3school.com.cn/soap/index.asp

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/pengchua/archive/2008/07/30/2740565.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章