Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Spring Tutorials >>> Spring Service >>> Web Service Example
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Interview Question
Description : SpringFramework
More...


Written By : Amit
Title :
Spring Remoting
Description : Java RMI Example
More...


Written By : ISHTEK
Title :
Spring Reflection
Description : POC Idea example code
More...


Written By : ISHTEK
Title :
Spring singleton
Description : Spring Container Example
More...


Written By : ISHTEK
Title :
Spring Features Updates News
Description : Bringing To Page
More...

Tags/Keywords : Spring Service, web service, spring web service, example
Author : ISHTEK
Date (Year/Month/Date): 2009-12-20 Example on Spring Framework Web Service - easy steps followed

Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this 
as it is without appropriate evaluation to their, so to say, 
specific programming context.

This page intends only to provide bit and piece of known ways  for
doing some sort of example and may not be fit for any other purpose.

To my understanding of web services, I can say that it has a server and a client component to test the server component. Server and client can be on any Platform and Technology, be it Java, .NET, C, C++, PHP and many more. Irrespective of Platform and Technology, both Client and Server should be able to communicate with each other. Now in order to understand how SpringFramework solves or provides webservice support, I tried with a very simple example whereby I should be able to quickly put various pieces of coding/files to create a server component using Spring-WS, main site I referred to start with is http://static.springsource.org/spring-ws/sites/1.5/
Advertisement :
The Spring Web Service software used in this example is
  • Spring-WS 1.5.8
  • I decided to use Apache AXIS 1.5.x version for building web service client in order to access this example Spring Web Service server component. Version of JDK used in this example is Java version "1.6.0_16". Before using Java version 1.6.x , one question I was having in my mind as does Spring-WS works under Java 1.6 version? Well my question got answered after I visited FAQ page from SpringSource.org http://static.springsource.org/spring-ws/sites/1.5/faq.html#java-1.6 Now I plan to use Apache Tomcat 5.5.9 as web server. I decided to use Contact-First way of implementing Spring Web Service, as i read about it from the documentation from SpringSource.org. While creating a web service using Spring-WS 1.5.8, I have created following separate items first:
  • Web service Interface - SimpleService.java
  • Web service Implementation - SimpleServiceImpl.java
  • Created a WSDL file- simple.wsdl
  • One can create a WSDL file from scratch, manually of course, but I tried a different way to create this WSDL file by using Apache AXIS2 1.5.x version, "java2wsdl"
  • Web service End Point - SimpleEndPoint.java
  • Spring Framework - applicationContext.xml
  • Spring WS Servlet related configuration - simple-ws-servlet.xml
  • Web Application Descriptor file - web.xml
  • This example has two Java files, one interface and another implementation class file for this interface. example.ExampleService
    package example;
    
    public interface ExampleService {
    	public String reflectMyWords(String argStr);
    }
    
    example.ExampleServiceImpl
    package example;
    public class ExampleServiceImpl implements ExampleService
    {
    	public String reflectMyWords(String argStr) {
    		System.out.println("inside the ExampleServiceImpl,
    		              text from caller program "+ argStr);
    		return "Got your message, Thanks";
    	}
    }
    
    java2wsdl -o . -of example.wsdl -cn example.ExampleService After running the above command, I was able to generate following WSDL file for this example: example.wsdl
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://example" 
    xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
    targetNamespace="http://example">
        <wsdl:types>
            <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://example">
                <xs:element name="reflectMyWords">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="reflectMyWordsResponse">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:schema>
        </wsdl:types>
        <wsdl:message name="reflectMyWordsRequest">
            <wsdl:part name="parameters" element="ns:reflectMyWords"/>
        </wsdl:message>
        <wsdl:message name="reflectMyWordsResponse">
            <wsdl:part name="parameters" element="ns:reflectMyWordsResponse"/>
        </wsdl:message>
        <wsdl:portType name="ExampleServicePortType">
            <wsdl:operation name="reflectMyWords">
                <wsdl:input message="ns:reflectMyWordsRequest" wsaw:Action="urn:reflectMyWords"/>
                <wsdl:output message="ns:reflectMyWordsResponse" wsaw:Action="urn:reflectMyWordsResponse"/>
            </wsdl:operation>
        </wsdl:portType>
        <wsdl:binding name="ExampleServiceSoap11Binding" type="ns:ExampleServicePortType">
            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
            <wsdl:operation name="reflectMyWords">
                <soap:operation soapAction="urn:reflectMyWords" style="document"/>
                <wsdl:input>
                    <soap:body use="literal"/>
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal"/>
                </wsdl:output>
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:binding name="ExampleServiceSoap12Binding" type="ns:ExampleServicePortType">
            <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
            <wsdl:operation name="reflectMyWords">
                <soap12:operation soapAction="urn:reflectMyWords" style="document"/>
                <wsdl:input>
                    <soap12:body use="literal"/>
                </wsdl:input>
                <wsdl:output>
                    <soap12:body use="literal"/>
                </wsdl:output>
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:binding name="ExampleServiceHttpBinding" type="ns:ExampleServicePortType">
            <http:binding verb="POST"/>
            <wsdl:operation name="reflectMyWords">
                <http:operation location="ExampleService/reflectMyWords"/>
                <wsdl:input>
                    <mime:content type="text/xml" part="reflectMyWords"/>
                </wsdl:input>
                <wsdl:output>
                    <mime:content type="text/xml" part="reflectMyWords"/>
                </wsdl:output>
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:service name="ExampleService">
            <wsdl:port name="ExampleServiceHttpSoap11Endpoint" binding="ns:ExampleServiceSoap11Binding">
                <soap:address location="http://localhost:8080/axis2/services/ExampleService"/>
            </wsdl:port>
            <wsdl:port name="ExampleServiceHttpSoap12Endpoint" binding="ns:ExampleServiceSoap12Binding">
                <soap12:address location="http://localhost:8080/axis2/services/ExampleService"/>
            </wsdl:port>
            <wsdl:port name="ExampleServiceHttpEndpoint" binding="ns:ExampleServiceHttpBinding">
                <http:address location="http://localhost:8080/axis2/services/ExampleService"/>
            </wsdl:port>
        </wsdl:service>
    </wsdl:definitions>
    
    From the above autogenerated WSDL file , I changed some of the items, those I don't need in this example, and this way I could make this example WSDL file easier to understand. I changed "soap12:address location" value to http://localhost:8080/sample2/example.wsdl Now the modified WSDL file "example.wsdl"
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:ns="http://example" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    targetNamespace="http://example">
        <wsdl:types>
            <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://example">
                <xs:element name="reflectMyWords">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="0" name="args0" nillable="true" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="reflectMyWordsResponse">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:schema>
        </wsdl:types>
        <wsdl:message name="reflectMyWordsRequest">
            <wsdl:part name="parameters" element="ns:reflectMyWords"/>
        </wsdl:message>
        <wsdl:message name="reflectMyWordsResponse">
            <wsdl:part name="parameters" element="ns:reflectMyWordsResponse"/>
        </wsdl:message>
        <wsdl:portType name="ExampleServicePortType">
            <wsdl:operation name="reflectMyWords">
                <wsdl:input message="ns:reflectMyWordsRequest" wsaw:Action="urn:reflectMyWords"/>
                <wsdl:output message="ns:reflectMyWordsResponse" wsaw:Action="urn:reflectMyWordsResponse"/>
            </wsdl:operation>
        </wsdl:portType>
        <wsdl:binding name="ExampleServiceSoap11Binding" type="ns:ExampleServicePortType">
            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
            <wsdl:operation name="reflectMyWords">
                <soap:operation soapAction="urn:reflectMyWords" style="document"/>
                <wsdl:input>
                    <soap:body use="literal"/>
                </wsdl:input>
                <wsdl:output>
                    <soap:body use="literal"/>
                </wsdl:output>
            </wsdl:operation>
        </wsdl:binding>
        <wsdl:service name="ExampleService">
            <wsdl:port name="ExampleServiceHttpSoap11Endpoint" binding="ns:ExampleServiceSoap11Binding">
                <soap:address location="http://localhost:8080/sample2/example.wsdl"/>
            </wsdl:port>
        </wsdl:service>
    </wsdl:definitions>
    
    These files are generated by Java2WSDL (From Apache Axis2 1.5.x) and then manually edited file. In order to deploy this example of Spring Web service to my dev environment Tomcat web server, I created following folder structure inside webapps folder. sample2 sample2->WEB-INF->wsdl->example.wsdl sample2->WEB-INF->lib-> (Jar files from the Spring-WS-1.5.8 and SpringFraework 3.0.0) sample2->WEB-INF->classes->example->ExampleService.class sample2->WEB-INF->classes->example->ExampleServiceImpl.class sample2->WEB-INF->classes->example->ExampleEndPoint.class (this end point should have code/logic for calling appropriate service method and returning appropriate DOM Element) sample2->WEB-INF->applicationContext.xml (defines the bean having End Point) sample2->WEB-INF->example-ws-servlet.xml (I define end point mapping, endpoint, service impl, wsdl file definition) sample2->WEB-INF->web.xml ExampleEndPoint Java file has the ExampleService private variable and overidden invokeInternal method. protected Element invokeInternal(Element requestElement, Document document) throws Exception { example.ExampleEndPoint
    package example;
    
    import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
    import org.w3c.dom.Document;
    import org.w3c.dom.Text;
    import org.w3c.dom.Node;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    
    public class ExampleEndPoint extends AbstractDomPayloadEndpoint {
    
        public static final String NAMESPACE_URI_VALUE = "http://example";
        public static final String NS_RESPONSE_NAME = "reflectMyWordsResponse";
    
        private ExampleService exampleService;
    
        public void setExampleService(ExampleService exampleService) {
            this.exampleService = exampleService;
        }
    
        protected Element invokeInternal(Element requestElement, 
    	                                 Document document) throws Exception {
            String echo = exampleService.
    		            reflectMyWords(requestElement.getTextContent().trim());
            Element responseElement = document.createElementNS(NAMESPACE_URI_VALUE,
    		                                         NS_RESPONSE_NAME);
            Text responseText = document.createTextNode(echo);
            responseElement.appendChild(responseText);
            return responseElement;
        }
    }
    
    This invokeInternal method takes the requestElement and retrieves appropriate text content and passes it as argument to the example service method reflectMyWords and prepares an Element and returns it I have managed to create applicationContext.xml and example-ws-servlet.xml file that works with this example. For your reference as follows: 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.5.xsd">
    
    <bean class="example.ExampleEndPoint"/>
    </beans>
    
    AND example-ws-servlet.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="loadingEndPointMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
            <property name="defaultEndpoint" ref="exampleEndpoint"/>
        </bean>
    
        <bean id="exampleEndpoint" class="example.ExampleEndPoint">
            <property name="exampleService" ref="exampleService"/>
        </bean>
    
    
        <bean id="exampleService" class="example.ExampleServiceImpl">
        </bean>
        <bean id="example" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
          <constructor-arg value="/WEB-INF/wsdl/example.wsdl"/>
        </bean>
    
    </beans>
    
    After deploying this example in Tomcat web server, then on successful start of the example web application, one can check the runtime WSDL file by accessing the URL http://localhost:8080/sample2/example.wsdl In order to test this web service using Spring Web Service 1.5.8 version, I used SOAP UI and tested it to have the request and response SOAP XML files as follows: (These request and ressponse are created using SOAP UI) SOAP REQUEST
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:exam="http://example">
       <soapenv:Header/>
       <soapenv:Body>
          <exam:reflectMyWords>
             <!--Optional:-->
             <exam:args0>gggg</exam:args0>
          </exam:reflectMyWords>
       </soapenv:Body>
    </soapenv:Envelope>
    
    And the SOAP Response as follows:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header/>
       <SOAP-ENV:Body>
          <reflectMyWordsResponse xmlns="http://example">Got your message, Thanks</reflectMyWordsResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    
    And some system out println in the web server output console as well. This is a very simple example with most of the features are listed in this page. Your comments are welcome. Thanks.
    Advertisement :
    
    
    	

    Commented By ->
    Suresh Babu
    Nice example!!
    I am able to run my first Spring Web service within minutes.
    Great work.
    Are you interested in solving a very interesting Technology Stack while Playing this Game          

    Please write your Comment on this Matter
    (This will be visible if found suitable):
    Name: *
    Email (will not be displayed): *
    Matter: *
    30,40
    Enter bigger number from above :*
    Home >>> Spring Tutorials >>> Spring Service >>> Web Service Example
    Visitor/User referred related external URL:
    (Visible upon review and approved by this site Administrator)
    
    Referred By Name *:
    Resource URL *: (e.g, URL should be starting with http://www.-----.---)
     
    Resource Short Description *:
    12,20
    Enter bigger number from above : *

    Please log in to add or reply to any matter<- requires login
    Log in or Register
    Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
    CONTACT    PRIVACY POLICY    DISCLAIMER
    Terms of Use and Disclaimer :
    
    This web site provides some of the information about various technologies, example 
    code, tips, tutorials etc. Like any printed materials, content of these pages may 
    become out of date over a period of time. Therefore all visitor/users of this web 
    site are requested/advised to refer to the originating parties/sources for the 
    latest changes and happenings for detailed information. This information is not 
    intended to be a substitute for the original reference provided by the originating 
    parties/sources.
    
    By accessing and using this website in any ways, including, without
    limitation, browsing the website pages, using any information, using any content and/or 
    downloading any materials, you agree to and are bound by the terms of use 
    described in this page and Usage Terms and Conditions. 
    If you do not agree to all of 
    the terms and conditions contained in the terms of use described in this
    page and Usage Terms and Conditions, do not use this 
    website in any manner. If you are using the website on behalf of your 
    employer, you represent that you are authorized to accept these Terms of Use 
    on your employer's behalf.
    
    All Trademarks are property of their respective owner. Appropriate measure is being
    taken for providing accurate and up-to-date information but like any printed materials,
    these blog(s)/contents may eventually be outdated one day, so if you are using any 
    of these information, please refer original content/documentation from respective sources. 
    And under no circumstances shall the Author of these contents and/or this web site
    be liable for any loss, damage, expense incurred or suffered which is claimed to have
    occurred because of usage of the contents of this web site.
    If you have any questions/queries/feedback/suggestions then please write to this web
    site owner at contact.