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