eclipse+webservice开发实例
1.參考文獻:
1.利用Java編寫簡單的WebService實例 ?http://nopainnogain.iteye.com/blog/791525
2.Axis2與Eclipse整合開發Web Service ?http://tech.ddvip.com/2009-05/1242968642120461.html
3.http://blog.csdn.net/lightao220/article/details/3489015
4.http://clq9761.iteye.com/blog/976029
5.使用Eclipse+Axis2+Tomcat構建Web Services應用(實例解說篇)
2.實例1(主要看到[2])
2.1.系統功能:?
開發一個計算器服務CalculateService,這個服務包括加(plus)、減(minus)、乘(multiply)、除(divide)的操作。2.2.開發前準備:
2.3.開發前配置:
在Eclipse的菜單條中,Window --> Preferences --> Web Service --> Axis2?Perferences,在Axis2 runtime location中選擇Axis2解壓縮包的位置,設置好后,點"OK"即行。(如圖)
2.4.開發Web Service:
(1)新建一個Java Project,命名為"WebServiceTest1"
(2)新建一個class,命名為"CalculateService",完整代碼例如以下:
(4)下一步(next),在出現的Web Services對象框,在Service implementation中點擊"Browse",進入Browse Classes對象框,查找到我們剛才寫的寫的CalculateService類。(例如以下圖)。點擊"ok",則回到Web Service話框。
(5)在Web Service對話框中,將Web Service type中的滑塊,調到"start service“的位置,將Client?type中的滑塊調到"Test client"的位置。
(6)在Web Service type滑塊圖的右邊有個"Configuration",點擊它以下的選項,進入Service Deployment Configuration對象框,在這里選擇對應的Server(我這里用Tomcat6.0)和Web Service runtime(選擇Apache Axis2),例如以下圖:
(7)點OK后,則返回到Web Service對話框,同理,Client type中的滑塊右邊也有"Configuration",也要進行對應的置,步驟同上。完畢后,Next --> next即行。進入到Axis2 Web Service Java Bean Configuration,我們選擇Generate a default services.xml,例如以下圖所看到的:
(8)到了Server startup對話框,有個按鍵"start server"(例如以下圖),點擊它,則可啟動Tomcat服務器了。
(9)等啟完后,點擊"next -- > next",一切默認即行,最后,點擊完畢。最后,出現例如以下界面:(Web?Service Explorer),我們在這里便可測試我們的Web服務。(使用瀏覽器打開的話使用例如以下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。例如以下圖所看到的:
注:在瀏覽器中打開Web Service Explorer(有時候在eclipse中關閉了webservice explorer,能夠用這樣的方法打開)
首先登錄地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然后在網頁右上角選擇Web Service Exoplorer標簽。然后輸入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。這個wsdl地址就是我們剛才公布服務的那個wsdl。點擊go,例如以下圖所看到的:
然后就能夠看到例如以下界面了:
(10)測試比較簡單,比如,我們選擇一個"plus"的Operation(必須是CalculateServiceSoap11Binding),出現下圖,在x的輸入框中輸入1,在y的輸入框中輸入2,點擊"go",便會在status欄中顯示結果3.0。其它方法的測試也類似。結果如上圖所看到的。
2.5.CalculateServiceclient調用程序
前面我們已經定義好了加減乘除的方法并將這些方法公布為服務,那么如今要做的就是調用這些服務就可以。client調用程序例如以下代碼所看到的: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方式調用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定調用WebService的URLEndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/CalculateService");options.setTo(targetEPR);// 指定要調用的計算機器中的方法及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方法的參數值為兩個,各自是加數和被加數Object[] opAddEntryArgs = new Object[] { 1,2 };// 指定plus方法返回值的數據類型的Class對象Class[] classes = new Class[] { float.class };// 調用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]);} }執行結果: 3.0 -1.0 2.0 0.53.實例2.HelloService
(1)首先定義服務方法,代碼例如以下所看到的:
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將這種方法公布為服務。(3)編寫client代碼調用WebService(主要參考[5])
本文樣例與其它樣例最大的不同就在這里,其它樣例一般須要依據剛才的服務wsdl生成clientstub,然后通過stub來調用服務,這樣的方式顯得比較單一,client必須須要stub存根才可以訪問服務,非常不方面。本樣例的client不採用stub方式,而是一種實現通用的調用方式,不須要不論什么client存根就可以訪問服務。僅僅須要指定對于的web servce地址、操作名、參數和函數返回類型就可以。代碼例如以下所看到的:
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 {// 設定操作的名稱QName opQName = new QName(targetNamespace, opName);// 設定返回值// Class<?>[] opReturn = new Class[] { opReturnType };// 操作須要傳入的參數已經在參數中給定,這里直接傳入方法中調用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 stubfinal 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,表明客戶端調用成功。該樣例最大的不同和優勢表如今客戶端的調用方式,或者說是發起服務調用的方式,盡管比起客戶端stub存根的方式,代碼稍多,可是這樣的方式統一,不須要生產stub存根代碼,攻克了客戶端有非常多類的問題。假設讀者對這些代碼進一步封裝,我想調用方式非常easy,僅僅須要傳遞相關參數,這更好地說明了服務調用的優勢。并且這樣的方式更加簡單明了,一看便知詳細含義。而不須要弄得stub類的一些機制。(4)改寫client調用服務的代碼
(3)中提到的client應用代碼寫的稍微有些繁雜,以下將上面的client調用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方式調用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定調用WebService的URLEndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");options.setTo(targetEPR);// 指定要調用的sayHelloToPerson方法及WSDL文件的命名空間QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew");// 指定sayHelloToPerson方法的參數值Object[] opAddEntryArgs = new Object[] { "xuwei" };// 指定sayHelloToPerson方法返回值的數據類型的Class對象Class[] classes = new Class[] { String.class };// 調用sayHelloToPerson方法并輸出該方法的返回值System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);} }轉載于:https://www.cnblogs.com/gcczhongduan/p/4061795.html
總結
以上是生活随笔為你收集整理的eclipse+webservice开发实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国现代远程与继续教育网 统考 大学英
- 下一篇: 三种加密算法和两种密钥交换机制讲解