Springws 简明教程

Spring WS - Writing Client

在本章中,我们将学习如何使用 Spring WS 为 Spring WS - Writing Server 中创建的 Web 应用程序服务器创建客户端。

In this chapter, we will learn how to create a client for the web application server created in the Spring WS - Writing Server using Spring WS.

Step

Description

1

Update the project countryService under the package com.tutorialspoint as explained in the Spring WS – Writing Server chapter.

2

Create CountryServiceClient.java under the package com.tutorialspoint.client and MainApp.java under the package com.tutorialspoint as explained in the following steps.

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 = "http://localhost: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());
   }
}

Start the Web Service

启动 Tomcat 服务器并确保我们可以使用标准浏览器从 webapps 文件夹访问其他网页。

Start the Tomcat server and ensure that we can access other webpages from the webapps folder using a standard browser.

Test Web Service Client

右键单击 Eclipse 中应用程序中的 MainApp.java 并使用 run as Java Application 命令。如果应用程序一切正常,它将打印以下消息。

Right click on the MainApp.java in your application under Eclipse and use run as Java Application command. If everything is ok with the application, it will print the following message.

Country : United States
Capital : Washington
Population : 46704314
Currency : USD

在这里,我们已经为基于 SOAP 的 Web 服务创建了一个客户端– CountryServiceClient.java 。MainApp 使用 CountryServiceClient 对 Web 服务进行访问,发出 post 请求并获取数据。

Here, we have created a Client – CountryServiceClient.java for the SOAP based web service. MainApp uses CountryServiceClient to make a hit to the web service, makes a post request and gets the data.