web service入門
阿新 • • 發佈:2017-12-13
creat class 訪問 soa cat pan axis sys hello
1.首先創建一個webservice服務,然後發布出去;
2.然後創建一個webServiceClient客戶端去訪問;
(1)創建一個Dynamic Web Project
(2)創建包名,新建類,編寫方法;
package com.zhouy.web; public class webServiceDemo { public void sayHello(){ System.out.println("hello webservice!"); } public String sayToSomebody(String str){return "Hello "+str+",welcome to webservice!"; } }
(3)選中這個剛才新建的類,右鍵new-->創建Web Service
然後WebContent文件夾下就會出現一個wsdl文件夾,裏邊自動生成了一個.wsdl文件;
然後將這個webservice發布出去;點擊項目右鍵-->Run onserver;
然後在瀏覽器中輸入http://localhost:8080/webService01/services/webServiceDemo?wsdl;
出現如下所示證明此webservice接口發布成功.
(4)創建客戶端調用此webservice接口;
繼續創建一個Dynamic Web Project,新建包名;
選中包名然後右鍵創建Web Service Client;
輸入webservice接口地址 http://localhost:8080/webService01/services/webServiceDemo?wsdl;
然後會自動創建出這些類和接口;
這裏采用用兩種代碼測試,其實原理都是一樣,從類中跟進去會發現代碼都一個意思;
測試1:
package com.zhouy.test; import org.apache.axis.client.Call; import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType; public class AxisTest { public static void main(String[] args) { try { String str = "zhouy" ; String endpoint = "http://localhost:8080/webService01/services/webServiceDemo?wsdl" ; Service service = new Service() ; Call call = (Call) service.createCall() ; call.setTargetEndpointAddress(endpoint) ; call.setOperationName("sayToSomebody") ;//ws方法名 //一個輸入參數,如果方法有多個參數,復制多條該代碼即可,參數傳入下面new Object後面 call.addParameter("parameter1",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN) ; // call.addParameter("parameter2",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN) ; call.setReturnType(XMLType.XSD_STRING) ; call.setUseSOAPAction(true) ; // call.setSOAPActionURI("http://172.20.35.123:6110/cea-ws/services/ChangeMemberPinService"); String result = (String) call.invoke(new Object[]{str}) ; System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
測試2:
package com.zhouy.test; import java.rmi.RemoteException; import com.zhouy.web.WebServiceDemo; import com.zhouy.web.WebServiceDemoProxy; public class test { public static void main(String[] args) { // TODO Auto-generated method stub WebServiceDemo wd = new WebServiceDemoProxy().getWebServiceDemo(); try { // String str = wd.sayToSomebody("zhouy"); // System.out.println(str); wd.sayHello(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }View Code
web service入門