開發工具:MyEclipse 6.0

開發環境:

1.     jdk1.5

2.     CXF框架,版本apache-cxf-2.2.3.zip,到http://cxf.apache.org/download.html下載

注:如使用jdk1.6進行開發,需下載jaxb-api.jarjaxws-api.jar,然后在本機安裝JDK的地方,在jdk1.6.0jre文件夾下的lib文件夾中新建endorsed文件夾,放入以上兩個jar包才可以進行開發

第一步,先在MyEclipse新建一個java項目,項目名為HelloWebService。

第二步,在項目中引入apache-cxf-2.2.3.ziplib下的所有jar包。

第三步,編寫測試用接口以及其實現類:

 

接口:

 

 

Java代碼
  1. package test;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. public interface Hello {  
  6.       
  7.     public String sayHello(String str);  
  8.   
  9. }  

實現類:

 

Java代碼
  1. package test;  
  2.   
  3. public class HelloImpl implements Hello {  
  4.   
  5.     public String sayHello(String str) {  
  6.           
  7.         System.out.println("調用成功");  
  8.         return "Hello " + str;  
  9.     }  
  10.   
  11. }  

 

在接口中添加WebService的注解,將其標注為WebService的服務接口。

 

 

Java代碼
  1. @WebService  
  2. public interface Hello {  

第四步,編寫WebService的服務器端。

Java代碼
  1. package test;  
  2.   
  3. import org.apache.cxf.endpoint.Server;  
  4. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  5.   
  6. public class MainServer {  
  7.       
  8.     public static void main(String[] args) {  
  9.         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();  
  10.         factory.setAddress("http://localhost:8080/HelloWebService");  
  11.         factory.setServiceClass(HelloImpl.class);  
  12.         Server server = factory.create();  
  13.         server.start();  
  14.     }  
  15. }  

factory.setAddress("http://localhost:8080/HelloWebService");

設置服務在服務器上部署的位置

 

factory.setServiceClass(HelloImpl.class);

設置服務暴露的接口實現類

 

完成之后運行MainServer中的main方法。

 

注:因為CXF框架中有Jetty 6 服務器,所以這個的demo發布在其中運行。

 

之后打開瀏覽器,輸入:

http://localhost:8080/HelloWebService?wsdl

如能看見以下畫面則WebService發布成功:

 

Xml代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. - <wsdl:definitions name="HelloImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  3. - <wsdl:types>  
  4. - <xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0" xmlns:tns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  5.   <xs:element name="sayHello" type="tns:sayHello" />   
  6.   <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />   
  7. - <xs:complexType name="sayHello">  
  8. - <xs:sequence>  
  9.   <xs:element minOccurs="0" name="arg0" type="xs:string" />   
  10.   </xs:sequence>  

第五步,編寫客戶端

Java代碼
  1. package test;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. public class Client {  
  6.     public static void main(String[] args) {  
  7.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  8.         factory.setServiceClass(Hello.class);  
  9.         factory.setAddress("http://localhost:8080/HelloWebService");  
  10.         Hello hello = (Hello)factory.create();  
  11.           
  12.         System.out.println(hello.sayHello("weberyb"));  
  13.     }  
  14.   
  15. }  

 

factory.setServiceClass(Hello.class);

設置訪問服務器端的指定接口。

 

factory.setAddress("http://localhost:8080/HelloWebService");

設置訪問的服務的地址。

 

factory.create()

創建代理對象以供遠程調用

 

之后運行Clientmain方法,可以在控制臺的服務器端看見如下輸出:

Java代碼
  1. ....................  
  2. 2009-8-13 14:00:42 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass  
  3. 信息: Creating Service {http://test/}HelloService from class test.Hello  
  4. Hello weberyb  

 

說明客戶端調用WebService成功。

至此,這個簡單的WebService開發完畢
嘗試過jdk1.6   它的包會變成import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; 并且沒有factory.setAddress這個方法 采用自帶的例子進行測試 會出現參數null錯誤,但是客戶端調用服務器端都調用成功,可能原因是沒有加載那兩個JAR文件的問題,暫時沒有測試