標籤: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項目