Eclipse+Webservice簡單開發(fā)實例
1.實例1(主要看到[2])
1.1.系統(tǒng)功能:
開發(fā)一個計算器服務(wù)CalculateService,這個服務(wù)包含加(plus)、減(minus)、乘(multiply)、除(divide)的操作。
1.2.開發(fā)前準(zhǔn)備:
安裝Eclipse-jee;
下載最新版本的Axis2,網(wǎng)址http://axis.apache.org/axis2/java/core/download.cgi ,選擇Standard Binary Distribution的zip包,解壓縮得到的目錄名axis2-1.4.1,目錄內(nèi)的文件結(jié)構(gòu)如下:
1.3.開發(fā)前配置:
在Eclipse的菜單欄中,Window --> Preferences --> Web Service --> Axis2 Perferences,在Axis2 runtime location中選擇Axis2解壓縮包的位置,設(shè)置好后,點"OK"即行。(如圖)
1.4.開發(fā)Web Service:
(1)新建一個Java Project,命名為"WebServiceTest1"
(2)新建一個class,命名為"CalculateService",完整代碼如下:
package edu.sjtu.webservice; /** * 計算器運算 * @author rongxinhua */ public class CalculateService { //加法 public float plus(float x, float y) { return x + y; } //減法 public float minus(float x, float y) { return x - y; } //乘法 public float multiply(float x, float y) { return x * y; } //除法 public float divide(float x, float y) { if(y!=0) { return x / y; } else return -1; } }
(3)在"WebServiceTest1"項目上new --> other,找到"Web Services"下面的"Web Service";
(4)下一步(next),在出現(xiàn)的Web Services對象框,在Service implementation中點擊"Browse",進入Browse Classes對象框,查找到我們剛才寫的寫的CalculateService類。(如下圖)。點擊"ok",則回到Web Service話框。
(5)在Web Service對話框中,將Web Service type中的滑塊,調(diào)到"start service“的位置,將Client type中的滑塊調(diào)到"Test client"的位置。
(6)在Web Service type滑塊圖的右邊有個"Configuration",點擊它下面的選項,進入Service Deployment Configuration對象框,在這里選擇相應(yīng)的Server(我這里用Tomcat6.0)和Web Service runtime(選擇Apache Axis2),如下圖:
(7)點OK后,則返回到Web Service對話框,同理,Client type中的滑塊右邊也有"Configuration",也要進行相應(yīng)的置,步驟同上。完成后,Next --> next即行。進入到Axis2 Web Service Java Bean Configuration,我們選擇Generate a default services.xml,如下圖所示:
(8)到了Server startup對話框,有個按鍵"start server"(如下圖),點擊它,則可啟動Tomcat服務(wù)器了。
(9)等啟完后,點擊"next -- > next",一切默認即行,最后,點擊完成。最后,出現(xiàn)如下界面:(Web Service Explorer),我們在這里便可測試我們的Web服務(wù)。(使用瀏覽器打開的話使用如下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。如下圖所示:
注:在瀏覽器中打開Web Service Explorer(有時候在eclipse中關(guān)閉了webservice explorer,可以用這種方法打開)
首先登錄地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然后在網(wǎng)頁右上角選擇Web Service Exoplorer標(biāo)簽。然后輸入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。這個wsdl地址就是我們剛才發(fā)布服務(wù)的那個wsdl。點擊go,如下圖所示:
然后就可以看到如下界面了:
(10)測試比較簡單,例如,我們選擇一個"plus"的Operation(必須是CalculateServiceSoap11Binding),出現(xiàn)下圖,在x的輸入框中輸入1,在y的輸入框中輸入2,點擊"go",便會在status欄中顯示結(jié)果3.0。其他方法的測試也類似。結(jié)果如上圖所示。
1.5.CalculateService客戶端調(diào)用程序
前面我們已經(jīng)定義好了加減乘除的方法并將這些方法發(fā)布為服務(wù),那么現(xiàn)在要做的就是調(diào)用這些服務(wù)即可。客戶端調(diào)用程序如下代碼所示:CalculateServiceTest.java
package edu.sjtu.webservice.test; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class CalculateServiceTest { /** * @param args * @throws AxisFault */ public static void main(String[] args) throws AxisFault { // TODO Auto-generated method stub // 使用RPC方式調(diào)用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調(diào)用WebService的URL EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/WebServiceTest1/services/CalculateService"); options.setTo(targetEPR); // 指定要調(diào)用的計算機器中的方法及WSDL文件的命名空間:edu.sjtu.webservice。 QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法 QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//減法 QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法 QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//除法 // 指定plus方法的參數(shù)值為兩個,分別是加數(shù)和被加數(shù) Object[] opAddEntryArgs = new Object[] { 1,2 }; // 指定plus方法返回值的數(shù)據(jù)類型的Class對象 Class[] classes = new Class[] { float.class }; // 調(diào)用plus方法并輸出該方法的返回值 System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]); System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]); } }
運行結(jié)果:
3.0
-1.0
2.0
0.5
2.實例
2.HelloService
(1)首先定義服務(wù)方法,代碼如下所示:
package edu.sjtu.webservice; public class HelloService { public String sayHelloNew() { return "hello"; } public String sayHelloToPersonNew(String name) { if (name == null) { name = "nobody"; } return "hello," + name; } public void updateData(String data) { System.out.println(data + " 已更新。"); } }
(2)參考實例1將這個方法發(fā)布為服務(wù)。
(3)編寫客戶端代碼調(diào)用WebService(主要參考[5])
本文例子與其他例子最大的不同就在這里,其他例子一般需要根據(jù)剛才的服務(wù)wsdl生成客戶端stub,然后通過stub來調(diào)用服務(wù),這種方式顯得比較單一,客戶端必須需要stub存根才能夠訪問服務(wù),很不方面。本例子的客戶端不采用stub方式,而是一種實現(xiàn)通用的調(diào)用方式,不需要任何客戶端存根即可訪問服務(wù)。只需要指定對于的web servce地址、操作名、參數(shù)和函數(shù)返回類型即可。代碼如下所示:
HelloServiceTest2.java package edu.sjtu.webservice.test; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class HelloServiceTest2 { private RPCServiceClient serviceClient; private Options options; private EndpointReference targetEPR; public HelloServiceTest2(String endpoint) throws AxisFault { serviceClient = new RPCServiceClient(); options = serviceClient.getOptions(); targetEPR = new EndpointReference(endpoint); options.setTo(targetEPR); } public Object[] invokeOp(String targetNamespace, String opName, Object[] opArgs, Class<?>[] opReturnType) throws AxisFault, ClassNotFoundException { // 設(shè)定操作的名稱 QName opQName = new QName(targetNamespace, opName); // 設(shè)定返回值 // Class<?>[] opReturn = new Class[] { opReturnType }; // 操作需要傳入的參數(shù)已經(jīng)在參數(shù)中給定,這里直接傳入方法中調(diào)用 return serviceClient.invokeBlocking(opQName, opArgs, opReturnType); } /** * @param args * @throws AxisFault * @throws ClassNotFoundException */ public static void main(String[] args) throws AxisFault, ClassNotFoundException { // TODO Auto-generated method stub final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService"; final String targetNamespace = "http://webservice.sjtu.edu"; HelloServiceTest2 client = new HelloServiceTest2(endPointReference); String opName = "sayHelloToPersonNew"; Object[] opArgs = new Object[] { "My Friends" }; Class<?>[] opReturnType = new Class[] { String[].class }; Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType); System.out.println(((String[]) response[0])[0]); } }
運行該程序,點擊Run As->Java application,可以看到控制臺端口的輸出是:Hello, My Friends,表明客戶端調(diào)用成功。該例子最大的不同和優(yōu)勢表現(xiàn)在客戶端的調(diào)用方式,或者說是發(fā)起服務(wù)調(diào)用的方式,雖然比起客戶端stub存根的方式,代碼稍多,但是這種方式統(tǒng)一,不需要生產(chǎn)stub存根代碼,解決了客戶端有很多類的問題。如果讀者對這些代碼進一步封裝,我想調(diào)用方式很簡單,只需要傳遞相關(guān)參數(shù),這更好地說明了服務(wù)調(diào)用的優(yōu)勢。而且這種方式更加簡單明了,一看便知具體含義。而不需要弄得stub類的一些機制。
(4)改寫客戶端調(diào)用服務(wù)的代碼
(3)中提到的客戶端應(yīng)用代碼寫的略微有些繁雜,下面將上面的客戶端調(diào)用service程序進行改寫,簡潔了許多。代碼如下:
HelloServiceTest.java import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class HelloServiceTest { public static void main(String args[]) throws AxisFault { // 使用RPC方式調(diào)用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調(diào)用WebService的URL EndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService"); options.setTo(targetEPR); // 指定要調(diào)用的sayHelloToPerson方法及WSDL文件的命名空間 QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew"); // 指定sayHelloToPerson方法的參數(shù)值 Object[] opAddEntryArgs = new Object[] { "xuwei" }; // 指定sayHelloToPerson方法返回值的數(shù)據(jù)類型的Class對象 Class[] classes = new Class[] { String.class }; // 調(diào)用sayHelloToPerson方法并輸出該方法的返回值 System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]); } }
通過以上內(nèi)容給大家介紹了Eclipse+Webservice簡單開發(fā)實例,希望對大家有所幫助!
相關(guān)文章
2022?最新?IntelliJ?IDEA?詳細配置步驟演示(推薦)
作為一名開發(fā)人員,第一肯定是選擇一款趁手的開發(fā)利器,本人使用?Java?偏多,這里推薦使用?IntelliJ?IDEA,?俗稱神級開發(fā)工具,具體的安裝過程就不過多贅述了,有需要了解的朋友可以參考下本文2022-09-09如何基于spring security實現(xiàn)在線用戶統(tǒng)計
這篇文章主要介紹了如何基于spring security實現(xiàn)在線用戶統(tǒng)計,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06