Idea實現WebService實例 轉


作者:http://blog.csdn.net/dreamfly88/article/details/52350370

因為工作需要,數據傳輸部分需要使用webservice實現,經過兩天的研究,實現了一個簡單的例子,具體方法如下。

首先需要新建一個項目,如圖:

下一步點擊finish,然后會生成一個webservice項目,在HelloWorld類里面寫自己的方法,在file下編譯一下這個類,不編譯,idea會提示不通過,編譯后需要將為該服務發布WSDL文件,此文件必須生成,如下圖:

選擇需要發布的服務

然后部署到TOMCAT,如圖,這里需要注意的是需要引入這個庫才能正常運行webservice

啟動tomcat后,在瀏覽器中敲入如下代碼:localhost:8080/services 回車測試webservice是否部署成功:

然后編寫客戶端測試代碼,如下:

 

主要代碼:

服務端:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. package example;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. /** 
  6.  * Created by zhangqq on 2016/8/26. 
  7.  */  
  8.   
  9. public class HelloWorld {  
  10.   
  11.   public String sayTitle(String from) {  
  12.     String result = "title is " + from;  
  13.     System.out.println(result);  
  14.     return result;  
  15.   }  
  16.   
  17.   
  18.   public String sayBody(String Other) {  
  19.     String result = "-------------body is-------------- " + Other;  
  20.     System.out.println(result);  
  21.     return result;  
  22.   }  
  23.   
  24.   public String sayAll(String title,String body) {  
  25.     String result ="--------title:"+title+ "----------------/r/nbody:--------------------------- " + body;  
  26.     System.out.println(result);  
  27.     return result;  
  28.   }  
  29. }  


客戶端:

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. package test;  
  2.   
  3. import org.apache.axis.client.Call;  
  4. import org.apache.axis.client.Service;  
  5. import org.apache.axis.utils.StringUtils;  
  6.   
  7. import javax.xml.rpc.ServiceException;  
  8. import java.net.MalformedURLException;  
  9.   
  10. /** 
  11.  * Created by zhangqq on 2016/8/29. 
  12.  */  
  13. public class WebSvrClient {  
  14.   
  15.   
  16.     public static void main(String[] args) {  
  17.         String url = "http://localhost:8080/services/HelloWorldService";  
  18.         String method = "sayTitle";  
  19.         String[] parms = new String[]{"abc"};  
  20.         WebSvrClient webClient = new WebSvrClient();  
  21.   
  22.         String svrResult = webClient.CallMethod(url, method, parms);  
  23.   
  24.         System.out.println(svrResult);  
  25.     }  
  26.   
  27.     public String CallMethod(String url, String method, Object[] args) {  
  28.         String result = null;  
  29.   
  30.         if(StringUtils.isEmpty(url))  
  31.         {  
  32.             return "url地址為空";  
  33.         }  
  34.   
  35.         if(StringUtils.isEmpty(method))  
  36.         {  
  37.             return "method地址為空";  
  38.         }  
  39.   
  40.         Call rpcCall = null;  
  41.   
  42.   
  43.         try {  
  44.             //實例websevice調用實例  
  45.             Service webService = new Service();  
  46.             rpcCall = (Call) webService.createCall();  
  47.             rpcCall.setTargetEndpointAddress(new java.net.URL(url));  
  48.             rpcCall.setOperationName(method);  
  49.   
  50.             //執行webservice方法  
  51.             result = (String) rpcCall.invoke(args);  
  52.   
  53.         } catch (Exception e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         return result;  
  57.   
  58.     }  
  59. }  



實例地址:

源碼下載地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM