MyEclipse6.5上基於JAX-WS開發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.新建一個工程
開始我們新建一個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.

我們給這個工程取名爲WebServiceProject.注意JAX-WS支持只在javaEE5或更高版本的工程中是可行的。如果你需要使用低版本的工程類型(java1.4或者1.3),那麼只能使用XFire Web Service代替JAX-WS。
 
這裏我們使用上面的JAX—WS。
4.創建服務類
服務類就是一個普通的java類,負責提供我們想要發佈的執行方法。這裏我們寫一個簡單的計算器類,實現幾個典型的計算器應用方法,如加減乘除。
首先我們先建一個包,WebServiceProject->src->new->package,取名com.myeclipseide.ws
讓後我們在這個包下建一個類,Calculator.java.
根據上面提到的,這個計算器類實現計算器的加減乘除算法,簡單實現:
Java代碼
  1. package com.myeclipseide.ws;      
  2.     
  3. public class Calculator {      
  4.     public int add(int a, int b) {      
  5.         return (a + b);      
  6.     }      
  7.     
  8.     public int subtract(int a, int b) {      
  9.         return (a - b);      
  10.     }      
  11.     
  12.     public int multiply(int a, int b) {      
  13.         return (a * b);      
  14.     }      
  15.     
  16.     public int divide(int a, int b) {      
  17.         return (a / b);      
  18.     }      
  19. }     
可以看出,這個類中的方法是非常簡單的,沒有用到特殊的註釋還有接口,父類之類的東西。
5.創建一個Web Service
在上面的工具條中點擊新建Web Service
Note:如果沒有的話可以File->New->others->Myeclipse->WebService->webService
點擊之後出現的屏幕,在Strategy中選擇Bottom-up scenario,因爲我們已經建立好了Calculator類而且想根據它建立JAX-WS服務。
下面是創建的最後一個屏幕,你需要選擇提供webService方法的javaBean,在我們這個例子中就是我們已經建立好的Calculator類。
填好之後,Myeclipse會自動幫我們填滿其他的項,Select Generate WSDL in project and hit Finish.
點擊完成之後,Myeclipse會自動生成CalculatorDelegate代理類,還有一些必須的JAX-WS描述符,而且會自動在服務器目錄下的web.xml中配置WebService的一些mappings,方便將webService部署到服務器中。
到此web service已經建立好了,我們開始部署它然後進行測試。
6.部署和測試webService。
這裏我們不使用用Myeclipse自帶的tomcat服務器,使用自己應經在電腦中部署好的tomcat5.5。
在server面板中右擊,選擇configure
部署自己的tomcat注意選擇jdk要跟項目中的相同。
現在要向工程中導入JAX-WS的jar包
在項目名稱上右擊->properties->Add Library->Myeclipse Libraries->最後面的兩個。
點擊完成,導入成功。
Note:Myeclipse自帶的tomcat中有自帶的這兩個jar包,可以不用導入。

6.1部署
在部署好的tomcat服務器上右擊選擇Add Deployment
點擊完成。
6.2測試
運行tomcat服務器,在工具欄中點擊launch WebService Explorer
打開後,點擊右上角的WSDL視圖,可以看到下面的屏幕
在WSDL URL中填寫路徑:http://localhost:8888/WebServiceProject/CalculatorPort?WSDL
解釋下路徑組成:
http://localhost:8888/是服務器的路徑,我的端口號是8888,可以根據自己的更改,一般都是8080。
/WebServiceProject = We know by default the Web Context-root that is used to deploy(部署) web projects matches the name of the projects. (因爲我們沒有爲這個工程自定義我們的Web Context-root,所以他就是這個工程的名字)
/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代碼
  1. <servlet>    
  2.     <description>JAX-WS endpoint - CalculatorService</description>    
  3.     <display-name>CalculatorService</display-name>    
  4.     <servlet-name>CalculatorService</servlet-name>    
  5.     <servlet-class>    
  6.         com.sun.xml.ws.transport.http.servlet.WSServlet     
  7.     </servlet-class>    
  8.     <load-on-startup>1</load-on-startup>    
  9.   </servlet>    
  10.   <servlet-mapping>    
  11.     <servlet-name>CalculatorService</servlet-name>    
  12.     <url-pattern>/CalculatorPort</url-pattern>    
  13.   </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.
弄清楚之後,我們開始測試,比如我們選擇add方法:
填寫args,點擊go,在status中就會顯示結果。
結果是正確的。
7.創建Webservice Client
現在我們已經部署好Webservice,而且應經測試過了,那我們新建一個Webservice client,來調用Webservice提供的方法。
7.1新建一個java project,給他取個名字。比如我們叫它ClientofWebService
在工具條中點擊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
點擊Next知道完成。
可以看到在新建的java project ClientofWebService中,src文件夾下產生了許多的文件,根據名稱我們大體可以瞭解其意思,可以打開看一下源代碼,其實不難理解。比如 add文件,就是Calculator類中add方法的兩個參數的get和set方法。其他類似。
我們在文件夾下見一個類test.java寫一個main函數測試
Java代碼
  1. public static void main(String[] args) {      
  2.     /* Create the service instance */      
  3.     CalculatorService service = new CalculatorService();      
  4.     CalculatorDelegate delegate = service.getCalculatorPort();      
  5.     
  6.     /* Using the web service, perform the 4 calculations */      
  7.     System.out.println("1. 3+7=" + delegate.add(37));      
  8.     System.out.println("2. 12-2=" + delegate.subtract(122));      
  9.     System.out.println("3. 9*9=" + delegate.multiply(99));      
  10.     System.out.println("4. 40/2=" + delegate.divide(402));      
  11. }     

運行得到如下結果:
1. 3+7=10
2. 12-2=10
3. 9*9=81
4. 40/2=20
測試完成。
8. Resources
In this section we want to provide you with additional links to resources that supplement the topics covered in this tutorial. While this is not an exhaustive list, we do make an effort to point to the more popular links that should provide you with diverse, high-quality information.

Example Web Service Project & Web Service Client Project
NOTE: The example projects provided are configured to run against WebSphere 6.1. You may need to adjust the Target Server and/or the runtime JRE libraries used to build the projects to more closely match your particular build and deployment environment.

 

 

http://blog.csdn.net/foart/article/details/4287515

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章