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;
    }
	
}

More by this Author

  • Date Parsing And Date Formatting In JSP Using JSTL
    4

    I have faced problem and i found solution after lots of googling. So may be this will help others when they faced this kind of requirement. Many times we come across a situation where we want to create java.util.Date...

  • Spring Web Service
    13

    Spring Web Service I will assume that you are aware with Spring and basics of web service. I will show you how to develop web service using Spring. Spring uses contract-first approach for developing web service....


6 comments

Brian 7 years ago

Hi chirag,

When I compile, the code project, it says that it gives me these errors

[ERROR] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

[INFO] Compilation failure

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[11,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[12,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[13,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[14,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[15,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[12,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[13,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[14,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[15,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[16,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[12,33] pack

age javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[13,33] pack

age javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[14,33] pack

age javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[15,33] pack

age javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[12,33] packa

ge javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[13,33] packa

ge javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[14,33] packa

ge javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[15,33] packa

ge javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\ObjectFactory.java:[11,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[11,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[12,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[13,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[14,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[15,33] package

javax.xml.bind.annotation does not exist

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[38,1] cannot fi

nd symbol

symbol: class XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[39,1] cannot fi

nd symbol

symbol: class XmlType

@XmlType(name = "", propOrder = {

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[42,1] cannot fi

nd symbol

symbol: class XmlRootElement

@XmlRootElement(name = "TrainRequest")

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[42,1] cannot fi

nd symbol

symbol: class XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[43,1] cannot fi

nd symbol

symbol: class XmlType

@XmlType(name = "TrainDetails", propOrder = {

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[45,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainRequest

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[38,1] canno

t find symbol

symbol: class XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[39,1] canno

t find symbol

symbol: class XmlType

@XmlType(name = "PassengerDetails", propOrder = {

C:\testws\src\main\java\com\sample\tr\schemas\TrainRequest.java:[47,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainRequest

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[51,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainDetails

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[53,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainDetails

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[54,5] cannot fi

nd symbol

symbol : class XmlSchemaType

location: class com.sample.tr.schemas.TrainDetails

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[56,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainDetails

C:\testws\src\main\java\com\sample\tr\schemas\TrainDetails.java:[58,5] cannot fi

nd symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainDetails

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[45,5] canno

t find symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.PassengerDetails

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[39,1] cannot

find symbol

symbol: class XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[40,1] cannot

find symbol

symbol: class XmlType

@XmlType(name = "masterPassenger", propOrder = {

C:\testws\src\main\java\com\sample\tr\schemas\PassengerDetails.java:[47,5] canno

t find symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.PassengerDetails

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[47,5] cannot

find symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.MasterPassenger

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[49,5] cannot

find symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.MasterPassenger

C:\testws\src\main\java\com\sample\tr\schemas\MasterPassenger.java:[51,5] cannot

find symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.MasterPassenger

C:\testws\src\main\java\com\sample\tr\schemas\ObjectFactory.java:[28,1] cannot f

ind symbol

symbol: class XmlRegistry

@XmlRegistry

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[37,1] cannot f

ind symbol

symbol: class XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[38,1] cannot f

ind symbol

symbol: class XmlType

@XmlType(name = "", propOrder = {

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[41,1] cannot f

ind symbol

symbol: class XmlRootElement

@XmlRootElement(name = "TrainResponse")

C:\testws\src\main\java\com\sample\tr\schemas\TrainResponse.java:[44,5] cannot f

ind symbol

symbol : class XmlElement

location: class com.sample.tr.schemas.TrainResponse

C:\testws\src\main\java\com\sample\tr\schemas\package-info.java:[8,26] package j

avax.xml.bind.annotation does not exist

[INFO] ------------------------------------------------------------------------

[INFO] For more information, run Maven with the -e switch

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 4 seconds

[INFO] Finished at: Wed Sep 09 17:10:49 EAT 2009

[INFO] Final Memory: 5M/11M

[INFO] ------------------------------------------------------------------------

C:\testws>

I am wondering where do i need to place the com\sample\tr\schemas such that the files can see the package?

regards

Brian


chirag272003 profile image

chirag272003 7 years ago Author

Which version of jdk you are using?


Brian 7 years ago

I am using 1.5


Brian 7 years ago

Hi chirag,

I have been trying to solve the above problem on the client, but i havent yet succeeded, the problem is that the libs from C:\jaxb-ri-20090410\lib are not seen, i have placed the needed classes in the classpath, but still complains. i placed the com.sample.tr.schema which contains the generated javafiles from schema into the java folder of my client project, in case i did anything wrongly pse help me to solve this.

thanks

regards

Brian


Brian 6 years ago

Hi chirag,

Would you be having an idea about implementation of a webservice client with J2me?


Mahesh Kumar M 5 years ago

I used jaxb-api 2.2 version jar to resolve the error - "javax.xml.bind.annotation does not exist". It may be late but may be helpful for others hence i am have posted it here.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working