ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Web services implementation using spring framework with working example

Updated on February 3, 2012

Webservice Client Implementation

Spring framework provides an easy way to access a web service with the required input and to consume the web service response. The WebServiceGatewaySupport class in the spring framework is used to access the web service given the end point URL is injected to the object. To access the web service in a java class, it has to extend the above mentioned WebServiceGatewaySupport class. The required properties for this class will be injected using the dependency injection. The properties that are to be injected are as below

1. messageFactory ( This will be a reference to the spring AxiomSoapMessageFactory)

2. messageSender.

3. marshaller (This will be a reference to spring jaxb marshaller)

4. unmarshaller (This will be a reference to spring jaxb marshaller)

The spring configuration for injecting the dependencies is given below.

<alias alias="gateway" name="myGateway" />
	<bean id="myGateway" class="com.test.MyGateway">
                <property name="messageFactory" ref="soapMessageFactory"/>  	
		<property name="messageSender" ref="urlMessageSender"/>
		<property name="marshaller" ref="jaxbMarshaller"/>
		<property name="unmarshaller" ref="jaxbMarshaller"/>
		<property name="defaultUri" value="${MY_GATEWAY_ENPOINT_URI}"/>	

MY_GATEWAY_ENPOINT_URI given in the above code is a property what will be read from a property file under the class path.

The messageSender, messageFactory, marshaller and unmarshaller properties will be set as below.

<bean id="soapMessageFactory" class=""/>

<bean id="urlMessageSender" class="" scope="tenant">
  		<ref bean="httpClient"/>
        <property name="credentials">
                <bean class="org.apache.commons.httpclient.UsernamePasswordCredentials">
                    <constructor-arg value="test"/>
                    <constructor-arg value="tester"/>

    <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
  	<property name="params" ref="httpParams" />

	<bean id="httpParams" class="org.apache.commons.httpclient.params.HttpClientParams">		
		<property name="soTimeout" value="60" />

	<alias alias="unmarshaller" name="jaxbMarshaller"/>
	<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">        
        <property name="contextPath" value="com.test.jaxb.mygateway"/>        

The property "ContextPath" specified for the jaxbMarshaller is the package name for the jaxb objects.

The java class to access the web service will be as below.


public class MyGateWay extends WebServiceGatewaySupport {

	public Response getData(Request userData){
		Response response =(Response)getWebServiceTemplate().marshalSendAndReceive(userData);


    0 of 8192 characters used
    Post Comment

    No comments yet.