- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Spring_Web_Service_Client
I will assume that you have created Spring Web service and you are aware of how to create Spring Web Service.
If you are not familiar with Spring web service then go through below link
http://hubpages.com/hub/Spring_Web_Service
Let me start with Spring Web Service Client:
1) First add following dependencies in your pom file(I am using maven).
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testws</groupId>
<artifactId>testws</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>testws</name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.6.2</version>
</dependency>
<!-- Removed JDOM dependency since it is no more used -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.xml.soap</groupId>
<artifactId>saaj-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerVersion>1.6</compilerVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>Generate Jaxb classes from XSD using jaxb. Use this command in your web service client project at java folder to create JaxB classes for railway service web service.
jaxb trainRequest.xsd
So you will get 7 classes/interfaces generated in com.sample.tr.schemas folder.
2) Now its time to write context file for Spring Web Service Client.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="http://localhost:8080/railwayService" />
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.sample.tr.schemas.MasterPassenger</value>
<value>com.sample.tr.schemas.ObjectFactory</value>
<value>com.sample.tr.schemas.PassengerDetails</value>
<value>com.sample.tr.schemas.TrainDetails</value>
<value>com.sample.tr.schemas.TrainRequest</value>
<value>com.sample.tr.schemas.TrainResponse</value>
</list>
</property>
<property name="schema" value="classpath:trainRequest.xsd"/>
</bean>
</beans>In above context file first we have specified location url and marshaller technique(in our case its JAXB). Put XSD file in resources folder and give path to xsd file in Jaxb marshaller property called schema.
3) Now its time to read context file and pass required request elements and print response.
import java.math.BigInteger;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.client.core.WebServiceTemplate;
import com.sample.tr.schemas.MasterPassenger;
import com.sample.tr.schemas.PassengerDetails;
import com.sample.tr.schemas.TrainDetails;
import com.sample.tr.schemas.TrainRequest;
import com.sample.tr.schemas.TrainResponse;
public class RailwayWSClientContext {
public static void callMe() throws Exception{
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
WebServiceTemplate template = (WebServiceTemplate)ctx.getBean("webServiceTemplate");
TrainResponse resp = (TrainResponse)template.marshalSendAndReceive(retrieveRequest());
System.out.println(resp.getCode());
}
public static void main(String[] args) throws Exception {
callMe();
}
public static TrainRequest retrieveRequest(){
TrainRequest request = new TrainRequest();
PassengerDetails details = new PassengerDetails();
TrainDetails trainDetails = new TrainDetails();
trainDetails.setFrom("Pune");
trainDetails.setJourneyDate(prepareXMLGregorianCalendar(new Date(2009,11,5)));
trainDetails.setTo("Mumbai");
trainDetails.setTrainNumber(new BigInteger("1333"));
MasterPassenger masterPassenger = new MasterPassenger();
masterPassenger.setAge(new BigInteger("15"));
masterPassenger.setName("Chirag");
masterPassenger.setSex("Male");
details.setMasterPassenger(masterPassenger);
details.setNumberOfPassengers(new BigInteger("1"));
request.setPassengerDetails(details);
request.setTrainDetails(trainDetails);
return request;
}
public static XMLGregorianCalendar prepareXMLGregorianCalendar(Date date)
{
GregorianCalendar cal = (GregorianCalendar)GregorianCalendar.getInstance();
cal.setTime(date);
XMLGregorianCalendar dateTimeXML = null;
try
{
dateTimeXML = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
}
catch (DatatypeConfigurationException e)
{
}
return dateTimeXML;
}
}
