|
|
Web Services using apache axis2 報告人:高明權 DATE :2010/05/02 Released V1.00 BLOG:http://tommykao.blogspot.com EMAIL : MCK6214@gmail.com
Web Services Agenda Background Case 01: Say Hello, pojo Case 02: File Upload & Download Case 03: Using the SoapMonitor module Case 04: SSL Transport Security Case 05: Message Level Security Case 06: Customized Module Case 07: Stateful Case 08: Clustering 2 著作人:高明權
Case 02: File upload & Download Sample code: webservices_workspace_02.zip 3 著作人:高明權
2nd Web Service Project 建立 Java 專案 選單[File]/[New]/[Other…] 選擇[Java Project]/[Next] 輸入專案名稱後點選[Finish] 專案名稱: WebServicesTutorial 滑鼠右鍵[New]/[Folder] 建立 META-INF 目錄 著作人:高明權 4
File Transmission Classes - Download FileTransmitServices - Interfaces FileTransmitServices - downloadFile public byte[] downloadFile(String filename) { String filepath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + filename; byte[] result = null; int data; try { FileInputStream fis = new FileInputStream(filepath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((data = fis.read())!=-1) { baos.write(data); } fis.close(); result = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { } return result; } 著作人:高明權 5
File Transmission Classes - Upload FileTransmitServices - uploadFile public boolean uploadFile(byte[] datapart, String filename) { String filepath = System.getProperty("java.io.tmpdir") +System.getProperty("file.separator")+filename; FileOutputStream fos = null; boolean result = false; try { fos = new FileOutputStream(filepath); fos.write(datapart); fos.close(); result = true; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (Exception e) { } } } return result; } FileTransmitServices - uploadFileWithDataHandler public boolean uploadFileWithDataHandler(DataHandler handler, String filename) { String filepath = System.getProperty("java.io.tmpdir") +System.getProperty("file.separator")+filename; FileOutputStream fos = null; boolean result = false; try { fos = new FileOutputStream(filepath); this.saveUploaddedFile(handler.getInputStream(), fos); fos.close(); result = true; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (Exception e) { } } } return result; } 著作人:高明權 6
declare the File Transmission Classes as Web Services META-INF\Services.xml <serviceGroup> <!-- defines the name of the service. Axis2 uses the name of the service to create the endpoint address of the service as http://localhost:<port>/axis2/services/<nameofservice> So for the FileTransmitServices, the service endpoint will be http://localhost:8080/axis2/services/FileTransmitServices --> <service name="FileTransmitServices" scope="application"> <description>File Upload and Download Services</description> <!-- ServiceClass parameter specifies the service implementation class. --> <parameter name="ServiceClass">sample.webservice.case02.FileTransmitServices</parameter> <!-- The messageReceiver element defines the message receiver to be used for handling this operation. Axis2 provides two built-in MessageReceivers for In-Only and In-Out operations without data binding; org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver for the In-Only operation and org.apache.axis2.receivers.RawXMLINOutMessageReceiver for the In-Out operation. --> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </messageReceivers> </service> </serviceGroup> 著作人:高明權 7
Pack the Web Services 打包 Web Services 滑鼠右鍵[Export] 選定 [JAR file]/[Next] 指定檔案輸出路徑 AXIS2路徑: <<Tomcat 安裝路徑 >>\ \webapps\axis2\WEB-INF\services\WebServicesTutorial.aar 注意: 包名必須以 .aar 結尾 須含 META-INF\services.xml 著作人:高明權 8
Deploy the Web Services 佈署 File Transmission Services 連線到 AXIS2 管理介面 網址: http://localhost:8080/axis2/axis2-admin/ 上傳 Web Service 檔案包 (*.aar ),選擇[Tools]/[Upload Service] 指定檔案並點選[Upload] 連線並確認 File Transmission Services 已開放 網址: http://localhost:8080/axis2/services/listServices 查看 File Transmission Services 取得該 Web Service 的描述 (WSDL) http://<host>:<port>/axis2/services/<ServiceName>?wsdl 本例為: http://localhost:8080/axis2/services/FileTransmitServices?wsdl 著作人:高明權 9
Test the Web Services 執行 wsdl2java 產生 Client 端程式 指令:%AXIS2_HOME%\bin\wsdl2java.bat -p sample.webservice.case02.client -o client -uri http://localhost:8080/axis2/services/FileTransmitServices?wsdl 撰寫並執行測試程式 著作人:高明權 10
Test the FileUpload Web Service 1/2 下載並安裝 soapUI 工具 網址: http://www.soapui.org 用 SoapUI 連線測試 建立新專案,選擇 [File]/[New soapUI Project] 專案名稱 : FileUploadServices WSDL路徑 : http://localhost:8080/axis2/services/FileUploadServices?wsdl 展開服務項目並著手編輯 soap request message 送出 soap request message,並檢視回傳結果 著作人:高明權 11
以上為基礎,務必照表操課 Know-How, ExAct, Knowledge
| URL: |
No comments posted yet
Comments