
- Spring WS 教程
- Spring WS - 主頁
- Spring WS - 概述
- Spring WS - 環境設定
- Spring WS - 第一個應用程式
- Spring WS - 靜態 WSDL
- Spring WS - 編寫伺服器
- Spring WS - 單元測試伺服器
- Spring WS - 編寫客戶端
- Spring WS - 單元測試客戶端
- Spring WS 有用資源
- Spring WS - 快速指南
- Spring WS - 有用資源
- Spring WS - 討論
Spring WS - 編寫客戶端
本章中,我們將學習如何使用 Spring WS 為 Spring WS - 編寫伺服器 中建立的 Web 應用程式伺服器建立客戶端。
步驟 | 說明 |
---|---|
1 | 按照 Spring WS 編寫伺服器章節所述,更新包 com.tutorialspoint 下的國家伺服器專案。 |
2 | 按照以下步驟,在包 com.tutorialspoint.client 下建立 CountryServiceClient.java,在包 com.tutorialspoint 下建立 MainApp.java。 |
CountryServiceClient.java
package com.tutorialspoint.client; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; import com.tutorialspoint.GetCountryRequest; import com.tutorialspoint.GetCountryResponse; public class CountryServiceClient extends WebServiceGatewaySupport { public GetCountryResponse getCountryDetails(String country){ String uri = "https://:8080/countryService/"; GetCountryRequest request = new GetCountryRequest(); request.setName(country); GetCountryResponse response =(GetCountryResponse) getWebServiceTemplate() .marshalSendAndReceive(uri, request); return response; } }
MainApp.java
package com.tutorialspoint; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import com.tutorialspoint.client.CountryServiceClient; public class MainApp { public static void main(String[] args) { CountryServiceClient client = new CountryServiceClient(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath("com.tutorialspoint"); client.setMarshaller(marshaller); client.setUnmarshaller(marshaller); GetCountryResponse response = client.getCountryDetails("United States"); System.out.println("Country : " + response.getCountry().getName()); System.out.println("Capital : " + response.getCountry().getCapital()); System.out.println("Population : " + response.getCountry().getPopulation()); System.out.println("Currency : " + response.getCountry().getCurrency()); } }
啟動 Web 服務
啟動 Tomcat 伺服器,並確保可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。
測試 Web 服務客戶端
在 Eclipse 中右鍵單擊應用程式中的 MainApp.java,並使用 以 Java 應用程式執行 命令。如果一切正常,它將列印以下訊息。
Country : United States Capital : Washington Population : 46704314 Currency : USD
在此,我們為基於 SOAP 的 Web 服務建立了一個客戶端 – CountryServiceClient.java。MainApp 使用 CountryServiceClient 向 Web 服務提交請求,發出 post 請求並獲取資料。
廣告