Springws-write-client
提供:Dev Guides
Spring WS-ライティングクライアント
この章では、Spring WSを使用して、リンク:/springws/springws_write_server [Spring WS-Writing Server]で作成されたWebアプリケーションサーバーのクライアントを作成する方法を学びます。
Step | Description |
---|---|
1 | Update the project countryService under the package com.finddevguides as explained in the Spring WS – Writing Server chapter. |
2 | Create CountryServiceClient.java under the package com.finddevguides.client and MainApp.java under the package com.finddevguides as explained in the following steps. |
CountryServiceClient.java
package com.finddevguides.client;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import com.finddevguides.GetCountryRequest;
import com.finddevguides.GetCountryResponse;
public class CountryServiceClient extends WebServiceGatewaySupport {
public GetCountryResponse getCountryDetails(String country){
String uri = "http://localhost:8080/countryService/";
GetCountryRequest request = new GetCountryRequest();
request.setName(country);
GetCountryResponse response =(GetCountryResponse) getWebServiceTemplate()
.marshalSendAndReceive(uri, request);
return response;
}
}
MainApp.java
package com.finddevguides;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.finddevguides.client.CountryServiceClient;
public class MainApp {
public static void main(String[] args) {
CountryServiceClient client = new CountryServiceClient();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.finddevguides");
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ページにアクセスできることを確認します。
Webサービスクライアントのテスト
Eclipseの下のアプリケーションでMainApp.javaを右クリックし、「Javaアプリケーションとして実行」コマンドを使用します。 アプリケーションで問題がなければ、次のメッセージが出力されます。
Country : United States
Capital : Washington
Population : 46704314
Currency : USD
ここでは、SOAPベースのWebサービス用のクライアント- CountryServiceClient.java を作成しました。 MainAppは、CountryServiceClientを使用してWebサービスにヒットし、ポストリクエストを行い、データを取得します。