posts - 5,  comments - 6,  trackbacks - 0

          關(guān)鍵字: webservice

          1. Introduction
          This document will outline the process of developing a JAX-WS web service and deploying it using MyEclipse 6.5 to the internal MyEclipse Tomcat server. The web service used in this tutorial will be a very simple calculator service that provides add, subtract, multiply and divide operations to the caller.

          MyEclipse also supports developing web services using the existing XFire framework from previous MyEclipse releases. For folks needing to develop and deploy WebSphere JAX-RPC or WebSphere JAX-WS web services, please take a look at our MyEclipse Blue Edition of the MyEclipse IDE.

          Additional resources covering web service creation using JAX-RPC, JAX-WS or XFire are included in the Resources section of this document.


          2. System Requirements
          This tutorial was created with MyEclipse 6.5. If you are using another version of MyEclipse (possibly newer), most of these screens and instructions should still be very similar.

          If you are using a newer version of MyEclipse and notice portions of this tutorial looking different than the screens you are seeing, please let us know and we will make sure to resolve any inconsistencies.

          3.新建一個(gè)工程
          開(kāi)始我們新建一個(gè)Web Service Project工程File->New->Web Service Project(Optional Maven Support)

           
          Note:A JAX-WS web service can also be generated in any existing Java EE 5 web project.

          我們給這個(gè)工程取名為WebServiceProject.注意JAX-WS支持只在javaEE5或更高版本的工程中是可行的。如果你需要使用低版本的工程類型(java1.4或者1.3),那么只能使用XFire Web Service代替JAX-WS。

           

          這里我們使用上面的JAX—WS。
          4.創(chuàng)建服務(wù)類
          服務(wù)類就是一個(gè)普通的java類,負(fù)責(zé)提供我們想要發(fā)布的執(zhí)行方法。這里我們寫一個(gè)簡(jiǎn)單的計(jì)算器類,實(shí)現(xiàn)幾個(gè)典型的計(jì)算器應(yīng)用方法,如加減乘除。
          首先我們先建一個(gè)包,WebServiceProject->src->new->package,取名com.myeclipseide.ws

          讓后我們?cè)谶@個(gè)包下建一個(gè)類,Calculator.java.


          根據(jù)上面提到的,這個(gè)計(jì)算器類實(shí)現(xiàn)計(jì)算器的加減乘除算法,簡(jiǎn)單實(shí)現(xiàn):
          Java代碼

          package com.myeclipseide.ws; 

          public class Calculator 
          public int add(int a, int b) 
          return (a + b); 
          }
           

          public int subtract(int a, int b) 
          return (a - b); 
          }
           

          public int multiply(int a, int b) 
          return (a * b); 
          }
           

          public int divide(int a, int b) 
          return (a / b); 
          }
           
          }
           


          可以看出,這個(gè)類中的方法是非常簡(jiǎn)單的,沒(méi)有用到特殊的注釋還有接口,父類之類的東西。
          5.創(chuàng)建一個(gè)Web Service
          在上面的工具條中點(diǎn)擊新建Web Service

          Note:如果沒(méi)有的話可以File->New->others->Myeclipse->WebService->webService
          點(diǎn)擊之后出現(xiàn)的屏幕,在Strategy中選擇Bottom-up scenario,因?yàn)槲覀円呀?jīng)建立好了Calculator類而且想根據(jù)它建立JAX-WS服務(wù)。

          下面是創(chuàng)建的最后一個(gè)屏幕,你需要選擇提供webService方法的javaBean,在我們這個(gè)例子中就是我們已經(jīng)建立好的Calculator類。

          填好之后,Myeclipse會(huì)自動(dòng)幫我們填滿其他的項(xiàng),Select Generate WSDL in project and hit Finish.


          點(diǎn)擊完成之后,Myeclipse會(huì)自動(dòng)生成CalculatorDelegate代理類,還有一些必須的JAX-WS描述符,而且會(huì)自動(dòng)在服務(wù)器目錄下的web.xml中配置WebService的一些mappings,方便將webService部署到服務(wù)器中。

          到此web service已經(jīng)建立好了,我們開(kāi)始部署它然后進(jìn)行測(cè)試。
          6.部署和測(cè)試webService。
          這里我們不使用用Myeclipse自帶的tomcat服務(wù)器,使用自己應(yīng)經(jīng)在電腦中部署好的tomcat5.5。
          在server面板中右擊,選擇configure


          部署自己的tomcat注意選擇jdk要跟項(xiàng)目中的相同。

          現(xiàn)在要向工程中導(dǎo)入JAX-WS的jar包
          在項(xiàng)目名稱上右擊->properties->Add Library->Myeclipse Libraries->最后面的兩個(gè)。

          點(diǎn)擊完成,導(dǎo)入成功。
          Note:Myeclipse自帶的tomcat中有自帶的這兩個(gè)jar包,可以不用導(dǎo)入。

          6.1部署
          在部署好的tomcat服務(wù)器上右擊選擇Add Deployment


          點(diǎn)擊完成。
          6.2測(cè)試
          運(yùn)行tomcat服務(wù)器,在工具欄中點(diǎn)擊launch WebService Explorer

          打開(kāi)后,點(diǎn)擊右上角的WSDL視圖,可以看到下面的屏幕

          在WSDL URL中填寫路徑:http://localhost:8888/WebServiceProject/CalculatorPort?WSDL
          解釋下路徑組成:
          http://localhost:8888/是服務(wù)器的路徑,我的端口號(hào)是8888,可以根據(jù)自己的更改,一般都是8080。
          /WebServiceProject = We know by default the Web Context-root that is used to deploy(部署) web projects matches the name of the projects. (因?yàn)槲覀儧](méi)有為這個(gè)工程自定義我們的Web Context-root,所以他就是這個(gè)工程的名字)
          /CalculatorPort = As we saw from the last screenshot in Section #5, when our JAX-WS web service was generated, it was bound using a servlet-mapping in the web.xml file to the /CalculatorPort path.
          XML代碼
            

          <servlet>     
              
          <description>JAX-WS endpoint - CalculatorService</description>     
              
          <display-name>CalculatorService</display-name>     
              
          <servlet-name>CalculatorService</servlet-name>     
              
          <servlet-class>     
                  com.sun.xml.ws.transport.http.servlet.WSServlet      
              
          </servlet-class>     
              
          <load-on-startup>1</load-on-startup>     
            
          </servlet>     
            
          <servlet-mapping>     
              
          <servlet-name>CalculatorService</servlet-name>     
              
          <url-pattern>/CalculatorPort</url-pattern>     
            
          </servlet-mapping>    
          <servlet>  
              
          <description>JAX-WS endpoint - CalculatorService</description>  
              
          <display-name>CalculatorService</display-name>  
              
          <servlet-name>CalculatorService</servlet-name>  
              
          <servlet-class>  
                  com.sun.xml.ws.transport.http.servlet.WSServlet   
              
          </servlet-class>  
              
          <load-on-startup>1</load-on-startup>  
            
          </servlet>  
            
          <servlet-mapping>  
              
          <servlet-name>CalculatorService</servlet-name>  
              
          <url-pattern>/CalculatorPort</url-pattern>  
            
          </servlet-mapping>

           WSDL = This is a universal query string argument that can be added to the end of any web service which will tell the web service to return it's full WSDL to the caller. In this case, the WSDL is returned to our Web Services Explorer tool which loads it up, and displays the web services exposed operations to us.
          弄清楚之后,我們開(kāi)始測(cè)試,比如我們選擇add方法:
          填寫args,點(diǎn)擊go,在status中就會(huì)顯示結(jié)果。  

          結(jié)果是正確的。
          7.創(chuàng)建Webservice Client
          現(xiàn)在我們已經(jīng)部署好Webservice,而且應(yīng)經(jīng)測(cè)試過(guò)了,那我們新建一個(gè)Webservice client,來(lái)調(diào)用Webservice提供的方法。
          7.1新建一個(gè)java project,給他取個(gè)名字。比如我們叫它ClientofWebService


          在工具條中點(diǎn)擊new Web Service Client

          然后按照以下步驟操作:


          The last step of the web service client creation is to specify either a WSDL File or a WSDL URL for the wizard to retrieve the web service WSDL from. In our case we are using the URL and generate the client into the new package com.myeclipseide.ws.client:

          http://localhost:8888/WebServiceProject/CalculatorPort?WSDL


          點(diǎn)擊Next知道完成。
          可以看到在新建的java project ClientofWebService中,src文件夾下產(chǎn)生了許多的文件,根據(jù)名稱我們大體可以了解其意思,可以打開(kāi)看一下源代碼,其實(shí)不難理解。比如add文件,就是Calculator類中add方法的兩個(gè)參數(shù)的get和set方法。其他類似。
          我們?cè)谖募A下見(jiàn)一個(gè)類test.java寫一個(gè)main函數(shù)測(cè)試
          Java代碼     

          public static void main(String[] args) {    
              
          /* Create the service instance */    
              CalculatorService service 
          = new CalculatorService();    
              CalculatorDelegate delegate 
          = service.getCalculatorPort();    
            
              
          /* Using the web service, perform the 4 calculations */    
              System.out.println( 
          "1. 3+7=" + delegate.add(37));    
              System.out.println( 
          "2. 12-2=" + delegate.subtract(122));    
              System.out.println( 
          "3. 9*9=" + delegate.multiply(99));    
              System.out.println( 
          "4. 40/2=" + delegate.divide(402));    
          }
             

           運(yùn)行得到如下結(jié)果:
          1. 3+7=10
          2. 12-2=10
          3. 9*9=81
          4. 40/2=20
          測(cè)試完成。

          本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/foart/archive/2009/06/21/4287515.aspx

          posted on 2009-10-17 22:50 潯陽(yáng)江頭夜送客 閱讀(3016) 評(píng)論(6)  編輯  收藏 所屬分類: WebService

          FeedBack:
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)
          2011-09-14 17:25 | nishilaji
          垃圾內(nèi)容丟九江人的臉  回復(fù)  更多評(píng)論
            
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)
          2011-11-07 10:24 | gxylh
          @nishilaji
          樓上的太沒(méi)素質(zhì)了,人家辛辛苦苦寫的這么詳細(xì),對(duì)于初學(xué)者來(lái)說(shuō)就希望有這種細(xì)致的文檔介紹。在此我很感謝樓主,謝謝你,讓我很清楚明了的學(xué)會(huì)了這個(gè)實(shí)現(xiàn)方法!  回復(fù)  更多評(píng)論
            
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)
          2011-11-12 15:45 | whily
          我是新手,你寫的很好,一看就明白  回復(fù)  更多評(píng)論
            
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)[未登錄](méi)
          2012-08-07 17:12 | test
          內(nèi)容介紹的非常好,非常適合新人 。謝謝。。  回復(fù)  更多評(píng)論
            
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)[未登錄](méi)
          2012-08-07 17:13 | test
          順便再說(shuō)一下,1L純2B,大家不要理它~~~~~~~~  回復(fù)  更多評(píng)論
            
          # re: MyEclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)
          2012-08-14 10:10 | hhs
          為什么我用的MyEclipse9.0不能達(dá)到跟你一樣的效果呢?就是WSDL頁(yè)面時(shí)不會(huì)顯示方法名,更不用說(shuō)測(cè)試了!還有就是web service client測(cè)試的時(shí)候,我輸入的wsdl url總是提示錯(cuò)誤,求解啊!糾結(jié)了我一上午!用Xfire也是不好使!  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          myeclipse6.5上基于JAX-WS開(kāi)發(fā)Webservice(中文示例)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 远安县| 嘉善县| 昭平县| 平原县| 梁河县| 白银市| 玛沁县| 中宁县| 巧家县| 广水市| 平阳县| 盱眙县| 五寨县| 黔西县| 平遥县| 内江市| 突泉县| 南丹县| 麦盖提县| 托里县| 响水县| 博白县| 大同县| 南阳市| 始兴县| 高雄市| 陵水| 扎赉特旗| 繁昌县| 钟祥市| 五莲县| 罗平县| 琼海市| 兴仁县| 衡阳市| 闽侯县| 尼勒克县| 清水县| 舞阳县| 柳林县| 德江县|