WebService-----Axis2搭建WebService項目

來源:互聯網
上載者:User

標籤:web service   wsdl   java   eclipse   webservice   

步驟1:下載Axis2

選擇Standard Binary Distribution的zip包;解壓到任一路徑下



步驟2:配置Axis2

Window -> Preferences -> Web Service 



步驟3:開發WebService

1)、建立一個Dynamic Web Project,命名為“WebService”


2)、建立一個class,命名為“CalculateService”

package com;public class CalculateService {// 加法public float plus(float x, float y) {return x + y;}// 減法public float minus(float x, float y) {return x - y;}// 乘法public float multiply(float x, float y) {return x * y;}// 除法public float divide(float x, float y) {if (y != 0) {return x / y;} elsereturn -1;}}

3)、建立Web Service

右鍵“WebService”項目-> new -> other











啟動後,點擊next -> next;都預設即可;完成結果如下:



4)、測試web服務

在瀏覽器中輸入上步中web service exploer的地址:









步驟4:開發用戶端調用程式

1)、建立Java Project項目


2)、把Axis2安裝目錄下lib目錄下的所有jar包匯入到該工程中


3)、建立CalculateServiceTest類

package webServiceTest;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 {public static void main(String[] args) throws AxisFault {// 使用RPC方式調用WebServiceRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();// 指定調用WebService的URLEndpointReference targetEPR = new EndpointReference("http://localhost:8080/first/services/CalculateService");options.setTo(targetEPR);<span style="color:#ff6666;">// 指定要調用的電腦器中的方法及WSDL檔案的命名空間(其實就是http://調用方法所在類的所在包的名字):com</span>QName opAddEntry = new QName("http://com", "plus");// 加法QName opAddEntryminus = new QName("http://com", "minus");// 減法QName opAddEntrymultiply = new QName("http://com", "multiply");// 乘法QName opAddEntrydivide = new QName("http://com", "divide");// 除法// 指定plus方法的參數值為兩個,分別是加數和被加數Object[] opAddEntryArgs = new Object[] { 1, 2 };// 指定plus方法傳回值的資料類型的Class對象Class[] classes = new Class[] { float.class };// 調用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]);}}

4)、運行該項目即可看到控制台列印的結構;


WebService-----Axis2搭建WebService項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.