Service Consumer Code Implementation

Spring Web Service Client Code Implementation Details : This contains how to create a spring client and invoke spring WebService.


1 Create Project for Web Service Consumer
       Create a Java Project ‘HelloSpringServiceClient’






2 Setup of Spring Web Service libraries in eclipse project  
      Copy all libraries from ‘HelloSpringService\ WebContent \ WEB-INF \ lib’ to ‘lib’ folder and add in to class path.

3. Create Client Implementation Class

Create a java Class name as HelloSpringServiceClient
package com.poc.hello.spring.webservice.client;
import java.io.IOException;
import javax.xml.transform.Source;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.xml.transform.ResourceSource;
import org.springframework.xml.transform.StringResult;
public class HelloSpringServiceClient extends WebServiceGatewaySupport
{
        private static final Logger LOGGER = Logger.getLogger(HelloSpringServiceClient.class);
        private Resource request;
       
        public static void main(String[] args) throws IOException
        {
                LOGGER.info("Enter in to HelloSpringServiceClient.main()");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",                     HelloSpringServiceClient.class);
HelloSpringServiceClient helloSpringServiceClient = (HelloSpringServiceClient)        applicationContext.getBean("helloSpringServiceClient");
                helloSpringServiceClient.callWebservice();
                LOGGER.info("Exit from HelloSpringServiceClient.main()");
        }
               
        public void setRequest(Resource request)
        {
                this.request = request;
        }

         public void callWebservice() throws IOException
        {
                LOGGER.info("Enter in to HelloSpringServiceClient.callWebservice()");
                Source requestSource = new ResourceSource(request);
                StringResult result = new StringResult();
                //webservicetemplate is here!!!!
                getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, result);
                LOGGER.info("Result Data : "+result);
                LOGGER.info("Exit from HelloSpringServiceClient.callWebservice()");
        }      
}



4 Create Client Configuration File (Spring Based Client) 
        Create applicationContext.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloSpringServiceClient"   class="com.poc.hello.spring.webservice.client.HelloSpringServiceClient">
<property name="defaultUri" value="http://localhost:8080/HelloSpringService/webservice/hello" />
<property name="request" value="classpath:com/poc/hello/spring/webservice/client/SpringWSRequest.xml" />
       bean>
beans>



5 Create Request XML 

Crate a request xml name as ‘SpringWSRequest.xml’

xml version="1.0" encoding="UTF-8"?>
<HelloRequest xmlns="http://www.cognizant.com/ws/hello">
      Hello Spring Web Service Test
HelloRequest>



6 Setup of Web Service Description File for client implementation
       Copy WSDL (helloworld.wsdl) file to the same directory


From HelloSpringService\ WebContent \ WEB-INF \ helloworld.wsdl 

  • To current working directory
7 Test Spring Web Service Client 
            Run ‘HelloSpringServiceClient.java’ as a java Application


Overall Results ,  The solution as tested in this POC performed well throughout all testing.