English 中文(简体)
Spring WS - Writing Client
  • 时间:2024-11-03

Spring WS - Writing Cpent


Previous Page Next Page  

In this chapter, we will learn how to create a cpent for the web apppcation 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 CountryServiceCpent.java under the package com.tutorialspoint.cpent and MainApp.java under the package com.tutorialspoint as explained in the following steps.

CountryServiceCpent.java

package com.tutorialspoint.cpent;

import org.springframework.ws.cpent.core.support.WebServiceGatewaySupport;
import com.tutorialspoint.GetCountryRequest;
import com.tutorialspoint.GetCountryResponse;

pubpc class CountryServiceCpent extends WebServiceGatewaySupport {
   pubpc 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.cpent.CountryServiceCpent;

pubpc class MainApp {
   pubpc static void main(String[] args) {
      CountryServiceCpent cpent = new CountryServiceCpent();
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setContextPath("com.tutorialspoint");
      cpent.setMarshaller(marshaller);
      cpent.setUnmarshaller(marshaller);
      GetCountryResponse response = cpent.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

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

Test Web Service Cpent

Right cpck on the MainApp.java in your apppcation under Ecppse and use run as Java Apppcation command. If everything is ok with the apppcation, it will print the following message.

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

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

Advertisements