Service Provider Code Implementation

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.