WebService測試案例

 在瀏覽器中輸入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我們可以看到HelloWorldService對應的WSDL信息,閱讀這個WSDL文檔,我們可以知道HelloWorld的sayHelloWorld方法已經被成功地發佈爲Web Service了。只要拿到這個WSDL就可以開發相應的客戶端調用程序了。 
   1)通過WSDL文件生成客戶端調用程序 
       首先我們通過http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我們可以獲得WSDL文件HelloWorldService.wsdl,並將其放在src目錄下面,接着我們通過程序訪問該WSDL文件,並調用需測試的方法。此時測試類WebServiceClientTest.java的內容如下所示: 
Java代碼 
  1. package test;  
  2.   
  3. import org.codehaus.xfire.client.Client;  
  4. import org.springframework.core.io.ClassPathResource;  
  5. import org.springframework.core.io.Resource;  
  6. import webservice.HelloWorld;  
  7.   
  8. /** 
  9.  *Copyright2007GuangZhouAmigo. 
  10.  *Allrightreserved.    
  11.  *HelloWorld的webservice的測試類. 
  12.  *@author<a href="mailto:[email protected]">AmigoXie</a> 
  13.  *@version1.0 
  14.  *Creationdate:2007-9-16-下午05:36:05 
  15.  */  
  16. public class WebServiceClientTest {   
  17.     HelloWorld helloWorld = null;  
  18.   
  19.     public static void main(String[] args) throws Exception {  
  20.        WebServiceClientTest test = new WebServiceClientTest();  
  21.        test.testClient();  
  22.     }   
  23.       
  24.     public void testClient() throws Exception {  
  25.        String wsdl = "HelloWorldService.wsdl"//對應的WSDL文件  
  26.        Resource resource = new ClassPathResource(wsdl);   
  27.        Client client = new Client(resource.getInputStream(), null); //根據WSDL創建客戶實例  
  28.          
  29.        Object[] objArray = new Object[1];  
  30.        objArray[0] = "kk";  
  31.        //調用特定的Web Service方法  
  32.        Object[] results = client.invoke("sayHelloWorld", objArray);  
  33.        System.out.println("result: " + results[0]);  
  34.     }  
  35. }  


運行該類,可得到如下輸出結果: 

result: hello,kk 

可看出運行結果正確。 

2)根據服務地址創建客戶端調用程序 

     接着讓我們來看一個根據服務地址創建客戶端調用程序的例子。我們可以通過測試類來測試Web Service的正確性,下面讓我們來看一個簡單的測試類,首先我們在src/test目錄建立WebServiceClientTest.java文件,並在src目錄下建立客戶端調用的Spring配置文件client.xml。在client.xml配置文件中我們定義了一個testWebService的bean,該bean訪問wsdlDocumentUrl爲http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。該xml文件的詳細內容如下: 
Xml代碼 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.     <bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">  
  6.        <property name="serviceClass">            
  7.                <value>webservice.HelloWorld</value>         
  8.        </property>        
  9.         <property name="wsdlDocumentUrl">           
  10.               <value>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl</value>         
  11.         </property>        
  12.      </bean>  
  13. </beans>  


在WebServiceClientTest.java文件中獲得HelloWorld,並調用它的sayHelloWorld方法來完成測試,該類的詳細內容如下所示: 

Java代碼 
  1. package test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import webservice.HelloWorld;  
  6.   
  7. /** 
  8.  *HelloWorld的webservice的測試類. 
  9.  */  
  10. public class WebServiceClientTest {   
  11.     HelloWorld helloWorld = null;  
  12.   
  13.     public static void main(String[] args) {  
  14.        WebServiceClientTest test = new WebServiceClientTest();  
  15.        test.testClient();  
  16.     }   
  17.   
  18.     public void testClient() {  
  19.        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  20.               "client.xml");  
  21.        helloWorld = (HelloWorld) ctx.getBean("testWebService");  
  22.        System.out.println(helloWorld.sayHelloWorld("kk"));  
  23.     }  
  24. }  


在啓動webservice_helloworld工程的情況下,運行WebServiceClientTest類,可看到控制檯包含如下信息: 

    hello,kk 

    由此可看出調用Web Service成功。



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