Axis2与Eclipse整合开发
<DIV>[图解教程] Axis2与Eclipse整合开发Web Service之一:简单的计算服务例子<DIV class=Space>时间:2009-07-02 16:55 来源:未知 作者:admin </DIV>
<DIV class=SC><A href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" target=_blank><FONT color=crimson>CSDN</FONT></A> <A href=".:window.external.AddFavorite(location.href+'#sc','★'+document.title)" target=_blank><FONT color=blue>IE</FONT></A> <A href=".:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)" target=_blank><FONT color=darkorange>QQ</FONT></A> <A href=".:window.open('http://cang.baidu.com/do/add?it='+encodeURIComponent(document.title.substring(0,76))+'&iu='+encodeURIComponent(location.href)+'&fr=ien#nw=1','_blank','scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes'); void 0" target=_blank><FONT color=mediumblue>百度</FONT></A> <A href=".:q=(document.location.href);void(open('http://www.digbuzz.com/submit.php?url='+encodeURIComponent(q),'',''));" target=_blank><FONT color=mediumpurple>我挖</FONT></A> <A href=".:window.open('http://www.google.com/bookmarks/mark?op=add&bkmk='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));void(0)" target=_blank><FONT color=darkblue>Google</FONT></A> <A title=POCO网摘 href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://my.poco.cn/fav/storeIt.php?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&img=http://www.h-strong.com/blog/logo.gif','keyit','scrollbars=no,width=475,height=575,status=no,resizable=yes'));keyit.focus();"><FONT color=yellowgreen>POCO</FONT></A> <A title=Yahoo书签 href=".:window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+encodeURIComponent(location.href)+'&t='+encodeURIComponent(document.title));void(0)"><FONT color=red>Yahoo</FONT></A> <A title=新浪ViVi href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(vivi=window.open('http://vivi.sina.com.cn/collect/icollect.php?pid=28&title='+escape(d.title)+'&url='+escape(d.location.href)+'&desc='+escape(t),'vivi','scrollbars=no,width=480,height=480,left=75,top=20,status=no,resizable=yes'));vivi.focus();"><FONT color=orangered>新浪</FONT></A> <A title=365Key网摘 href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();"><FONT color=chartreuse>365Key</FONT></A> <A title=天极网摘 href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(yesky=window.open('http://hot.yesky.com/dp.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t)+'&st=2','yesky','scrollbars=no,width=400,height=480,left=75,top=20,status=no,resizable=yes'));yesky.focus();"><FONT color=goldenrod>天极</FONT></A> <A title=和讯网摘 href=".:t=document.title;u=location.href;e=document.selection?(document.selection.type!='None'?document.selection.createRange().text:''):(document.getSelection?document.getSelection():'');void(open('http://bookmark.hexun.com/post.aspx?title='+escape(t)+'&url='+escape(u)+'&excerpt='+escape(e),'HexunBookmark','scrollbars=no,width=600,height=450,left=80,top=80,status=no,resizable=yes'));"><FONT color=cornflowerblue>和讯</FONT></A> <A href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(blog=window.open('http://www5.bolaa.com/CommendBlog/SmallLogin.aspx?title='+escape(d.title)+'&newspath='+escape(d.location.href)+'&subtitle='+escape(t),'bolaa','width=400px,height=400px'));blog.focus();" target=_blank><FONT color=lightsteelblue>博拉</FONT></A> <A href=".:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(live=window.open('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url='+escape(d.location.href)+'&title='+escape(d.title)+'&top=1','live','scrollbars=no,status=no,resizable=yes'));live.focus();" target=_blank><FONT color=limegreen>Live</FONT></A> <A title=推荐到diglog href=".:///"><FONT color=indianred>奇客</FONT></A> <A href=".:window.open('http://www.xianguo.com/service/submitfav/?link='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));void(0)" target=_blank><FONT color=yellowgreen>鲜果</FONT></A> <A href="http://www.shouker.com/" target=_blank><FONT color=firebrick>收客</FONT></A> <A href=".:var d=document,w=window,f='http://fanfou.com/share',l=d.location,e=encodeURIComponent,p='?u='+e(l.href)+'&t='+e(d.title)+'&d='+e(w.getSelection?w.getSelection().toString():d.getSelection?d.getSelection():d.selection.createRange().text)+'&s=bm';a=function(){if(!w.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=0,width=600,height=400'))l.href=f+'.new'+p};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}void(0)" target=_blank><FONT color=darksalmon>饭否</FONT></A> <A href=".:var d=document,w=window,f='http://jiwai.de/wo/share/s',l=d.location,e=encodeURIComponent,p='?u='+e(l.href)+'&t='+e(d.title)+'&d='+e(w.getSelection?w.getSelection().toString():d.getSelection?d.getSelection():d.selection.createRange().text);a=function(){if(!w.open(f+p,'sharer','toolbar=0,status=0,resizable=0,width=540,height=310'))l.href=f+'.new'+p};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}void(0)" target=_blank><FONT color=dodgerblue>叽歪</FONT></A> <A href=".:q=(document.location.href);void(open('http://www.waakee.com/submit.php?url='+encodeURIComponent(q),'',''));" target=_blank><FONT color=slateblue>挖客</FONT></A> </DIV>
<DIV class=De.ion>核心提示:系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply) 、除(divide)的操作。 开发前准备: 1、安装Eclipse-jee; 2、下载Axis2的最新版本Axis2 1.4.1 Release,网址 http://ws.apache.org/axis2/download/1_4_1/do </DIV>
<P>系统功能: 开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply)</P>
<P>、除(divide)的操作。<BR><BR></P>
<P>开发前准备:<BR>1、安装Eclipse-jee;<BR>2、下载Axis2的最新版本Axis2 1.4.1 Release,网址</P>
<P><A href="http://ws.apache.org/axis2/download/1_4_1/download.cgi" target=_blank><FONT color=#1a8bc8>http://ws.apache.org/axis2/download/1_4_1/download.cgi</FONT></A> ,选择Standard Binary Distribution</P>
<P>的.zip包即"axis2-1.4.1-bin.zip"这个文件,解压缩得到的目录名axis2-1.4.1,目录内的文件结构如</P>
<P>下:<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ125T-0.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ125T-0.gif" .load="imgResize(this, 650);" ;></A></P>
<P><BR>开发前配置:在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 </P>
<P>Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图</P>
<P>)</P>
<P><a href="http://ajava.org/uploads/allimg/090702/1AZ114N-1.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ114N-1.gif" .load="imgResize(this, 650);" ;></A><BR><BR><BR>开发Web Service: <BR>1、新建一个Java Project,命名为"WS_01";<BR>2、新建一个class,命名为"CalculateService",完整代码如下:</P>
<DIV>
<DIV class=dp-highlighter>
<DIV class=bar></DIV>
<OL class=dp-j>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=keyword>package</SPAN><SPAN> rong.service; </SPAN></FONT></SPAN></LI>
<LI><SPAN> </SPAN></LI>
<LI class=alt><SPAN></SPAN><FONT color=#000000><SPAN class=comment>/** */</SPAN><SPAN class=comment>/** </SPAN> </FONT></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * 计算器运算 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * @author rongxinhua </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> */</SPAN><SPAN> </SPAN></FONT></SPAN></LI>
<LI><SPAN></SPAN><FONT color=#000000><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>class</SPAN><SPAN> CalculateService { </SPAN></FONT></LI>
<LI class=alt><SPAN> </SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=comment>/** */</SPAN><SPAN class=comment>/** </SPAN> </FONT></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * 加法运算 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @param x 被加数 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * @param y 加数 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @return x与y的和 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> */</SPAN><SPAN> </SPAN></FONT></SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>float</SPAN><SPAN> plus(</SPAN><SPAN class=keyword>float</SPAN><SPAN> x, </SPAN><SPAN class=keyword>float</SPAN><SPAN> y){ </SPAN></FONT></LI>
<LI class=alt><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>return</SPAN><SPAN> x + y ; </SPAN></FONT></LI>
<LI><SPAN> } </SPAN></LI>
<LI class=alt><SPAN> </SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=comment>/** */</SPAN><SPAN class=comment>/** </SPAN> </FONT></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * 减法运算 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @param x 被减数 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * @param y 减数 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @return x与y之差 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> */</SPAN><SPAN> </SPAN></FONT></SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>float</SPAN><SPAN> minus(</SPAN><SPAN class=keyword>float</SPAN><SPAN> x, </SPAN><SPAN class=keyword>float</SPAN><SPAN> y){ </SPAN></FONT></LI>
<LI class=alt><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>return</SPAN><SPAN> x - y ; </SPAN></FONT></LI>
<LI><SPAN> } </SPAN></LI>
<LI class=alt><SPAN> </SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=comment>/** */</SPAN><SPAN class=comment>/** </SPAN> </FONT></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * 乘法运算 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @param x 被乘数 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * @param y 乘数 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @return x与y的乘积 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> */</SPAN><SPAN> </SPAN></FONT></SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>float</SPAN><SPAN> multiply(</SPAN><SPAN class=keyword>float</SPAN><SPAN> x, </SPAN><SPAN class=keyword>float</SPAN><SPAN> y){ </SPAN></FONT></LI>
<LI class=alt><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>return</SPAN><SPAN> x * y ; </SPAN></FONT></LI>
<LI><SPAN> } </SPAN></LI>
<LI class=alt><SPAN> </SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=comment>/** */</SPAN><SPAN class=comment>/** </SPAN> </FONT></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * 除法运算 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @param x 被除数 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> * @param y 除数 </SPAN> </FONT></SPAN></LI>
<LI><SPAN><FONT color=#000000><SPAN class=comment> * @return x与y的商 </SPAN> </FONT></SPAN></LI>
<LI class=alt><SPAN><FONT color=#000000><SPAN class=comment> */</SPAN><SPAN> </SPAN></FONT></SPAN></LI>
<LI><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>public</SPAN><SPAN> </SPAN><SPAN class=keyword>float</SPAN><SPAN> divide(</SPAN><SPAN class=keyword>float</SPAN><SPAN> x, </SPAN><SPAN class=keyword>float</SPAN><SPAN> y){ </SPAN></FONT></LI>
<LI class=alt><SPAN> </SPAN><FONT color=#000000><SPAN class=keyword>return</SPAN><SPAN> x / y ; </SPAN></FONT></LI>
<LI><SPAN> } </SPAN></LI>
<LI class=alt><SPAN> </SPAN></LI>
<LI><SPAN>} </SPAN></LI></OL></DIV></DIV><BR>
<P>3、在"WS_01"项目上new --> other,找到"Web Services"下面的"Web Service";<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ15318-2.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ15318-2.gif" .load="imgResize(this, 650);" ;></A><BR>4、下一步(next),在出现的Web Services对象框,在Service implementation中点击"Browse",进入</P>
<P>Browse Classes对象框,查找到我们刚才写的写的CalculateService类。(如下图)。点击"ok",则回到</P>
<P>Web Service话框。<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ14633-3.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ14633-3.gif" .load="imgResize(this, 650);" ;></A><BR>5、在Web Service对话框中,将Web Service type中的滑块,调到"start service“的位置,将Client </P>
<P>type中的滑块调到"Test client"的位置。<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ1OY-4.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ1OY-4.gif" .load="imgResize(this, 650);" ;></A><BR>6、在Web Service type滑块图的右边有个"Configuration",点击它下面的选项,进入Service </P>
<P>Deployment Configuration对象框,在这里选择相应的Server(我这里用Tomcat6.0)和Web Service </P>
<P>runtime(选择Apache Axis2),如下图:<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ13c1-5.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ13c1-5.gif" .load="imgResize(this, 650);" ;></A><BR>7、点OK后,则返回到Web Service对话框,同理,Client type中的滑块右边也有"Configuration",也</P>
<P>要进行相应的置,步骤同上。完成后,Next --> next即行。</P>
<P>8、到了Server startup对话框,有个按键"start server"(如下图),点击它,则可启动Tomcat服务器</P>
<P>了。<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ110F-6.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ110F-6.gif" .load="imgResize(this, 650);" ;></A><BR>9、等启完后,点击"next -- > next",一切默认即行,最后,点击完成。最后,出现如下界面:(Web </P>
<P>Service Explorer),我们在这里便可测试我们的Web服务。<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ12048-7.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ12048-7.gif" .load="imgResize(this, 650);" ;></A><BR><BR>10、测试比较简单,例如,我们选择一个"plus"的Operation,出现下图,在x的输入框中输入2,在y的</P>
<P>输入框中输入3,点击"go",便会在status栏中显示结果5.0。其他方法的测试也类似。<BR><a href="http://ajava.org/uploads/allimg/090702/1AZ112a-8.gif" target="_blank"><IMG border=0 src="http://ajava.org/uploads/allimg/090702/1AZ112a-8.gif" .load="imgResize(this, 650);" ;></A></P>
<P>到这里用Axis2与Eclipse整合开发的Web Service的服务端和客户端就成功了。你也试试吧!<BR><SPAN style="COLOR: #ff00ff"><STRONG> 转自:<SPAN style="COLOR: #ff00ff"> <A href="http://www.blogjava.net/rongxh7" target=_blank><FONT color=#1a8bc8>http://www.blogjava.net/rongxh7</FONT></A></SPAN></STRONG></SPAN><BR></P><BR></DIV>
页:
[1]