Spring WS - Yazma İstemcisi

Bu bölümde, Spring WS kullanarak Spring WS - Yazma Sunucusunda oluşturulan web uygulama sunucusu için bir istemcinin nasıl oluşturulacağını öğreneceğiz .

Adım Açıklama
1 Spring WS - Yazma Sunucusu bölümünde açıklandığı gibi com.tutorialspoint paketi altında proje countryService'i güncelleyin.
2 Aşağıdaki adımlarda açıklandığı gibi com.tutorialspoint paketi altında com.tutorialspoint.client ve MainApp.java paketi altında CountryServiceClient.java oluşturun.

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());
   }
}

Web Hizmetini Başlatın

Tomcat sunucusunu başlatın ve diğer web sayfalarına webapps klasöründen standart bir tarayıcı kullanarak erişebildiğimizden emin olun.

Web Hizmeti İstemcisini Test Edin

Eclipse altındaki uygulamanızda MainApp.java'ya sağ tıklayın ve run as Java Applicationkomut. Uygulamayla ilgili her şey yolundaysa, aşağıdaki mesajı yazdıracaktır.

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

Burada bir Müşteri oluşturduk - CountryServiceClient.javaSOAP tabanlı web hizmeti için. MainApp, web hizmetine bir hit yapmak için CountryServiceClient kullanır, bir gönderi talebinde bulunur ve verileri alır.