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}"/>	
	</bean>

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="org.springframework.ws.soap.axiom.AxiomSoapMessageFactory"/>

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

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

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

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

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.

com.test.MyGateway

public class MyGateWay extends WebServiceGatewaySupport {

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

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article