![]() ![]() |
Home >> Spring Framework
>> spring-ws-example
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/
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
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 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 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 simplefied 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.
| ![]()
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||