開發(fā)工具:MyEclipse 6.0

開發(fā)環(huán)境:

1.     jdk1.5

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

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

第一步,先在MyEclipse新建一個(gè)java項(xiàng)目,項(xiàng)目名為HelloWebService。

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

第三步,編寫測(cè)試用接口以及其實(shí)現(xiàn)類:

 

接口:

 

 

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

實(shí)現(xiàn)類:

 

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

 

在接口中添加WebService的注解,將其標(biāo)注為WebService的服務(wù)接口。

 

 

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

第四步,編寫WebService的服務(wù)器端。

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");

設(shè)置服務(wù)在服務(wù)器上部署的位置

 

factory.setServiceClass(HelloImpl.class);

設(shè)置服務(wù)暴露的接口實(shí)現(xiàn)類

 

完成之后運(yùn)行MainServer中的main方法。

 

注:因?yàn)?span style="font-size: 14pt;">CXF框架中有Jetty 6 服務(wù)器,所以這個(gè)的demo發(fā)布在其中運(yùn)行。

 

之后打開瀏覽器,輸入:

http://localhost:8080/HelloWebService?wsdl

如能看見以下畫面則WebService發(fā)布成功:

 

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);

設(shè)置訪問服務(wù)器端的指定接口。

 

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

設(shè)置訪問的服務(wù)的地址。

 

factory.create()

創(chuàng)建代理對(duì)象以供遠(yuǎn)程調(diào)用

 

之后運(yùn)行Clientmain方法,可以在控制臺(tái)的服務(wù)器端看見如下輸出:

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  

 

說明客戶端調(diào)用WebService成功。

至此,這個(gè)簡(jiǎn)單的WebService開發(fā)完畢
嘗試過jdk1.6   它的包會(huì)變成import org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean; 并且沒有factory.setAddress這個(gè)方法 采用自帶的例子進(jìn)行測(cè)試 會(huì)出現(xiàn)參數(shù)null錯(cuò)誤,但是客戶端調(diào)用服務(wù)器端都調(diào)用成功,可能原因是沒有加載那兩個(gè)JAR文件的問題,暫時(shí)沒有測(cè)試