Java Articles | Spring Articles

Wednesday, May 25, 2011

Spring Webservice Provider(Client) - Part 2

This tutorial is the continuation of the previous tutorial which I wrote to create a Spring based Web Service Provider. This is basically to write a Client which can send an AddRequest.xml document to the web service and recieves a AddResponse.xml document from the provider.


Step1:
Create a request xml document and store it in C:\Adder directory. This is the document which is sent to the web service as a request.
request.xml
<?xml version="1.0" encoding="UTF-8" ?>
<hr:AddRequest xmlns:hr="http://mycompany.com/calc/schemas"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mycompany.com/calc/schemas WEB-INF/calculator.xsd">
<hr:Number1>15.0</hr:Number1>
<hr:Number2>5.0</hr:Number2>
</hr:AddRequest>

Step2:
Create a java project AdderWSClient and write the client class.Create a lib directory in the project. Copy all the jars from the provider project and place them in a lib directory. Also add the jars in the classpath of the project.Create a class with the name AdderWSClient, in com.adder.client package, which extends a ebServiceGatewaySupport a class from the spring framework.

Write a method simpleSendAndRecieve which will send the request to the webservice by reading the xml from the specified loacation and writing the response.xml to the specified location.
public void simpleSendAndReceive() {

StreamSource requestMessage = new StreamSource(new File(
"C:\\Adder\\request.xml"));
StreamResult responseMessage = new StreamResult(new File(
"C:\\Adder\\response.xml"));
getWebServiceTemplate().sendSourceAndReceiveToResult(requestMessage,
responseMessage);
}
Write the main method to load the spring context.
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"client.xml");

AdderWSClient client = (AdderWSClient) context.getBean("webServiceClient");

client.simpleSendAndReceive();
}
Full listing of AdderWSClient.java
package com.adder.client;

import java.io.File;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;

public class AdderWSClient extends WebServiceGatewaySupport {

public void simpleSendAndReceive() {

StreamSource requestMessage = new StreamSource(new File(
"D:\\request.xml"));
System.out.print(requestMessage);
StreamResult responseMessage = new StreamResult(new File(
"D:\\response1.xml"));
getWebServiceTemplate().sendSourceAndReceiveToResult(requestMessage,
responseMessage);
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"client.xml");

AdderWSClient c = (AdderWSClient) context.getBean("webServiceClient");

c.simpleSendAndReceive();
}
}
Step 3:
We need the client.xml which is a spring context file. So we will also write a spring context file.Where we provide the client class with the default uri where we can locate the web service. Code for client.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 id="webServiceClient" class="com.adder.client.AdderWSClient">
<property name="defaultUri"
value="http://localhost:8080/AdderWS/AddNumberService/" />
</bean>

</beans>

Place the client.xml in the src directory of your project and execute the main method. A response.xml will appear in C:\Adder directory.

That was all for Spring Web Services. We have many other ways of creating a Spring Web Service and Client. This was one of the simpler ways and easy to learn.

Hope it helped you in learning. Happy Learning :)