Tech I Enjoy Logo
Custom Search
   Log In    OR    Register  


Home >> Spring Framework >> spring-ws-example

Author: Tech I Enjoy !!!

Copyright Notice: All Rights Reserved with Author of this Page.


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/

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 (first create WSDL file) 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. If anything missed out , please let me know at techienjoy at yahoo . com

    Some of the other Articles you may would like to read :
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 2.
    Spring Batch Framework Example :
    Spring Batch Framework Example source code
     and Step by step code walkthrough
    Interview Questions with answer on Spring Framework :
    Interview Questions with answer on Spring Framework.
    Spring Framework Remote Session Bean :
    Spring Framework Example source code of
    Remote Session Bean.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 1.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework and use
    Current Session Context.
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework.
    Spring Framework MVC and Tiles :
    Spring Framework MVC and Tiles with Example 
    source code explained.
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework Part 2.
    Spring Framework Local Stateless Session bean :
    Spring Framework Local Stateless Session bean with
    example source code explained.
    Spring Framework Web Example and WSDL :
    Spring Framework Web Example source code
    with WSDL.Step by step code walkthrough
    Spring Framework Singleton Example :
    Spring Framework Singleton Example source code 
    compared with GOF Singleton.
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.
    Spring Framework Example :
    Spring Framework Example source code Explained.
    Step by step code walkthrough


    References :
    Tags: spring dao hibernate example
    Tags: spring hibernate dao example
    Tags: spring hibernate example 2
    Tags: spring hibernate example current session context
    Tags: spring hibernate example
    Tags: Spring Interview Questions Answer
    Tags: Spring Local SLBean
    Tags: Spring MVC Tiles2
    Tags: Spring Remote SLBean
    Tags: Spring Singleton GOF Singleton Difference
    Tags: spring web example
    Tags: spring ws example
    Tags: Spring
    Tags: springws example wsdl
    

    Any source code found on any of the page(s) in Techienjoy.com is associated with following notice:
     /*
     * TECHIENJOY.COM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
     * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TECHIENJOY.COM SHALL NOT BE LIABLE FOR
     * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
     * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
     *
     * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
     * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
     * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
     * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
     * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
     * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
     * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). TECHIENJOY.COM
     * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
     * HIGH RISK ACTIVITIES.
     */
    
    
    For any of the content, if you would like to bring it to notice for removal from this web site, please write to this web site administrator @ EMAIL-ID,
    with appropriate concern and supporting proof(s). After thorough review and if found genuine concern, we would take appropriate action and 
    remove disputed content from this web site within 24 hours starting from the time it has brought to our notice.
    
    
    
    The content provided in this page is not warranted and/or not guaranteed by techienjoy.com. techienjoy.com is not liable for any negative 
    consequences that may result/arise from implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    All contents of this site is/are written and provided on an "AS IS" basis, without WARRANTIES or conditions of any kind, either express
    or implied, including, without limitation, merchantability, or fitness for a particular purpose. You are solely responsible for determining 
    the appropriateness of using or refering this and assume any risks associated with this.
    
    This web site is optimized for learning and training. Examples might be simplified to improve reading and basic understanding only. 
    This web site content are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. 
    In spite of all precautions taken to avoid any typo in these pages, there might be some issues like grammatical mistakes and typos 
    being observed in these pages, techienjoy.com extends sincerest apologies to all our visitors for the same.
    
    While using this web site, you agree to have read and accepted our terms of use and privacy policy.
    
    

    Android Examples || Android Training || Struts 1 || Spring Framework || Software Architecture || Servlet || Log4j Framework || JSP || JSF || JPA || JMS || Hibernate Framework || Enterprise Java || EJB || Design Patterns || Build Tools/Frameworks || Android Platform || ESB || Struts 2 || Core Java

    © Copyright 2010-2014, TECHIENJOY.COM, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions