JDK 1.4 and Spring WebService

This blog is about spring web service for JDK 1.4 Environment . It contains details regarding service provider and service consumer creation in eclipse IDE. 


You can find more about this in “http://static.springsource.org/spring-ws/sites/1.5/”. This blog can use by technical staff and management to implement the Spring Web service with the help of Eclipse IDE.

The purpose of this to show: How Spring Web service is flexible and simple implementation over any application.


I am using Windows XP Operating System and Java 1.4.

Required Toolkit Bundle JDK 1.4
Software/Tools Required
 • JDK 1.4
 • Apache Tomcat 4.1
 • Eclipse Europa Version: 3.3.0
 • Spring Binary Distribution ( http://sourceforge.net/projects/springframework/files/spring-webservices/spring-webservices 1.5.4 )
spring-ws-1.5.4-with-dependencies.zip
spring-ws-1.5.4.zip

Eclipse Configurations

1.Set Java Compiler

[Window > Preferences > Java >Compiler > JDK compliance level >> Set 1.4]
2.Set JRE (Java Run Time Environment)
[Window > Preferences > Java > Installed JRE s >> Set JRE 1.4]
3.Set Tomcat (Run Time Environment)
[Window > Preferences > Tomcat > Set Tomcat 4.1]
[Window > Preferences > Server > Installed Run Time > Add and Set Tomcat 4.1]





Spring Web Service Code Implementation Details

To Create Project for Web Service Provider, please create a Dynamic Web Project, because spring webservice a web based service, internally it uses a specifice filter to interact with client application 




Spring Web Service Code Implementation Details



1. Create Project for Web Service Provider --> Create a Dynamic Web Project




2. Create WSDL 
         Create a WSDL File helloworld.wsdl

xml version="1.0" encoding="UTF-8" ?>







<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:schema="http://www.cognizant.com/ws/hello"
      xmlns:tns="http://www.cognizant.com/ws/hello/definitions"
      targetNamespace="http://www.cognizant.com/ws/hello/definitions">
   <wsdl:types>
      <schema xmlns="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.cognizant.com/ws/hello">
            <element name="HelloRequest" type="string" />
            <element name="HelloResponse" type="string" />
      schema>
   wsdl:types>

   <wsdl:message name="HelloRequest">
      <wsdl:part element="schema:HelloRequest" name="HelloRequest" />
   wsdl:message>
   <wsdl:message name="HelloResponse">
      <wsdl:part element="schema:HelloResponse" name="HelloResponse" />
   wsdl:message>

   <wsdl:portType name="HelloPortType">
        <wsdl:operation name="Hello">
           <wsdl:input message="tns:HelloRequest" name="HelloRequest" />
           <wsdl:output message="tns:HelloResponse" name="HelloResponse"/>
        wsdl:operation>
   wsdl:portType>

   <wsdl:binding name="HelloBinding" type="tns:HelloPortType">
      <soap:binding style="document"
                  transport="http://schemas.xmlsoap.org/soap/http"   />
      <wsdl:operation name="Hello">
            <soap:operation soapAction="" />
            <wsdl:input name="HelloRequest">
                  <soap:body use="literal" />
            wsdl:input>
            <wsdl:output name="HelloResponse">
                  <soap:body use="literal" />
            wsdl:output>
      wsdl:operation>
   wsdl:binding>
   <wsdl:service name="HelloSpringService">
      <wsdl:port binding="tns:HelloBinding" name="HelloPort">
         <soap:address
         location="http://localhost:8080/HelloSpringService/webservice"/>
      wsdl:port>
   wsdl:service>
wsdl:definitions>










3. Setup of Spring Web Service libraries in eclipse project     
         Copy all libraries present inside spring-ws-1.5.4.zip  and spring-ws-1.5.4-with-dependencies.zip to lib folder.
         (HelloSpringService\ WebContent \ WEB-INF \ lib )

Other Jar Settings
  • Delete servlet 2.5.jar
  • Add jsr173_1.0_api.jar File
  • Add saaj-impl-1.3.2.jar File and delete saaj-impl-1.3.jar
  • Add stax-api-1.0-2.jar File
  • Add xercesImpl.jar File
4. Create Service Interface
         Create an interface ‘HelloSpringService.java’.

package com.poc.hello.spring.webservice;
public interface HelloSpringService
       String hello(String name); 
               }



5. Create Service interface implementation class
         Create a class ‘HelloSpringServiceImpl.java’

package com.poc.hello.spring.webservice;
import org.apache.log4j.Logger;

public class HelloSpringServiceImpl implements HelloSpringService
{        
  private static final Logger LOGGER = Logger.getLogger(HelloSpringServiceImpl.class);
  public String hello(String name)
  { 
       LOGGER.debug("HelloSpringServiceImpl.hello() Request Parameter is : "+name);
       return "Hello, " + name + ",this is Sample POC for Spring Webservice."; 
  } 
    }



6. Create Service End Point implementation class
        Create an Endpoint class ‘HelloSpringEndPoint.java’

package com.poc.hello.spring.webservice;
import org.apache.log4j.Logger;
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
 * @author Vivek.Jain3@cognizant.com
 */
public class HelloSpringEndPoint extends AbstractDomPayloadEndpoint
{
   private static final Logger LOGGER = Logger.getLogger(HelloSpringEndPoint.class);
   //Namespace of both request and response.
   public static final String NAMESPACE_URI = "http://www.cognizant.com/ws/hello";
   //The local name of the expected request.
   public static final String HELLO_REQUEST_LOCAL_NAME = "HelloRequest";
   //The local name of the created response.
   public static final String HELLO_RESPONSE_LOCAL_NAME = "HelloResponse";
   private HelloSpringService helloSpringService;     
   protected Element invokeInternal(Element requestElement, Document document)throws Exception
   {
       LOGGER.info("Enter into HelloEndPoint.invokeInternal()");
       NodeList children = requestElement.getChildNodes();
       Text requestText = null;
       for (int i = 0; i < children.getLength(); i++)
       {
              if (children.item(i).getNodeType() == Node.TEXT_NODE)
              {
                     requestText = (Text) children.item(i);
                     break;
              }
       }
       if (requestText == null)
       {
              throw new IllegalArgumentException("Could not find request text node");
       }

       String response = helloSpringService.hello(requestText.getNodeValue());
       Element responseElement = document.createElementNS(
                                  NAMESPACE_URI,HELLO_RESPONSE_LOCAL_NAME);
       Text responseText = document.createTextNode(response);
       responseElement.appendChild(responseText);
       LOGGER.info("Exit from HelloEndPoint.invokeInternal()");
       return responseElement;
       }

       public void setHelloSpringService(HelloSpringService helloSpringService)
       {
              this.helloSpringService = helloSpringService;
       }
}

7. Configure Deployment Descriptor

     Configure web.xml) file of web Project for Spring Web-Service

xml version="1.0" encoding="UTF-8"?>
DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
       <display-name>Spring Test WebServicedisplay-name>

<servlet>
       <servlet-name>hello-ws-springservlet-name>
       <servlet-class>
                 org.springframework.ws.transport.http.MessageDispatcherServlet
       servlet-class>
              <load-on-startup>-1load-on-startup>
       servlet>
       <servlet-mapping>
              <servlet-name>hello-ws-springservlet-name>
              <url-pattern>/webservice/*url-pattern>
       servlet-mapping>
web-app>


8. Configure Spring Web Service Deployment Descriptor


Configure hello-ws-spring-servlet.xml file of web Project for Spring Web-Service.

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">


9. Test Web service by ‘Web service Explorer’
           Deploy Web Application
           Run Server
           Run Web Service Explorer
                   Go to Run
                         Lunch the Web Service Explorer 
                           (Web Service Explorer Eclipse Plug-in to test Web Service)
          Find more about it in below link 
          http://www.eclipse.org/webtools/jst/components/ws/M4/tutorials/WebServiceExplorer.html


Click on WSDL Icon


Find/Insert WSDL Url





You can view the source also, by clicking source hyperlink.

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.

Comments

Popular Posts