使用axis2呼叫web service
阿新 • • 發佈:2018-12-12
使用axis2生成客戶端程式碼
- 下載axis2 , 點選這裡, 選擇 Binary distribution
- 下載後解壓,解壓進入到bin目錄
- 執行命令生成客戶端程式碼
-uri: 後可以接wsdl連線,或者wsdl檔案 -p : 指定生成的包名,可以和專案一致,比如你想將生成的程式碼放置於 com.example.test 裡面,那麼-p com.example.test -o : 指定生成程式碼存放在本地的路徑,看你個人喜好
- 生成客戶端程式碼後,將程式碼拷貝到你專案中
呼叫客戶端程式碼請求wsdl
部分程式碼經過了脫敏處理
String url = "http://localhost:8080/services/XXXService.svc"; // 下面的內容基本都是依葫蘆畫瓢即可 AxxServiceStub stub = new AxxServiceStub(url); Options options = stub._getServiceClient().getOptions(); options.setTimeOutInMilliSeconds(100000); stub._getServiceClient().setOptions(options); // 初始化請求入參 AxxServiceStub.InvestReq investReq = new AxxServiceStub.InvestReq(); // set請求欄位資料 investReq.setAccount(1); // 這裡需要留意一下 // Source是服務端自定義欄位 investReq.setSource(AxxServiceStub.ClientToInvestType.PC); AxxServiceStub.Invest invest = new AxxServiceStub.Invest(); invest.setReq(investReq); AxxServiceStub.InvestResp response = stub.invest(invest).getInvestResult(); log.info("呼叫xx返回結果:" + response.getMessage());