Monday, March 04, 2013

Spring WS JAXB Web Sevice Client

1. See the WSDL and schema in my previous post here
2. Define beans in applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" 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.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_11" />
</property>
</bean>

<bean id="XYZOrderServiceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.abc.XYZ.integration.test.jaxb" />
</bean>

<bean id="XYZOrderServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory" />
<property name="marshaller" ref="XYZOrderServiceMarshaller"></property>
<property name="unmarshaller" ref="XYZOrderServiceMarshaller"></property>
<property name="messageSender">
<bean
class="org.springframework.ws.transport.http.CommonsHttpMessageSender">
</bean>
</property>
<property name="defaultUri"
value="http://localhost:7001/XYZService/ws/XYZWebService.wsdl" />
</bean>

<bean id="XYZOrderServiceClient" class="com.abc.XYZ.integration.test.XYZOrderServiceClient">
<constructor-arg ref="XYZOrderServiceTemplate"></constructor-arg>
</bean>
</beans>

3. JUnit test case to invoke web service client

package com.abc.XYZ.integration.test;

import static org.junit.Assert.assertNotNull;

import org.junit.BeforeClass;
import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class XYZServiceClientTest {

private static ClassPathXmlApplicationContext context = null;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("/applicationContext.xml");
}

@Test
public void testPlaceOrder() {
XYZOrderServiceClient client = (XYZOrderServiceClient) context
.getBean("XYZOrderServiceClient");
String orderRef = client.placeOrder("offerId1");

assertNotNull(orderRef);
}
}

4. Service Client

package com.abc.XYZ.integration.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.ws.client.core.WebServiceTemplate;

import com.abc.XYZ.integration.test.jaxb.ObjectFactory;
import com.abc.XYZ.integration.test.jaxb.SendOrderRequest;
import com.abc.XYZ.integration.test.jaxb.SendOrderResponse;

public class XYZOrderServiceClient {

private static final Log logger = LogFactory.getLog(XYZOrderServiceClient.class);
private static final ObjectFactory WS_CLIENT_FACTORY = new ObjectFactory();

private WebServiceTemplate webServiceTemplate;

public XYZOrderServiceClient(WebServiceTemplate webServiceTemplate) {
this.webServiceTemplate = webServiceTemplate;
}

public String placeOrder(String offerId) {
logger.debug("Preparing PlaceOrderRequest.....");
SendOrderRequest request = WS_CLIENT_FACTORY.createSendOrderRequest();
request.setHHId(offerId);

logger.debug("Invoking Web service Operation[PlaceOrder]....");
SendOrderResponse response = (SendOrderResponse) webServiceTemplate
.marshalSendAndReceive(request);
logger.debug("Order reference:" + response.getField1());
return response.getField1();
}
}

5. maven POM.xml

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.XYZ.integration</groupId>
<artifactId>XYZtest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<scope>compile</scope>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>

<build>
<finalName>XYZServiceTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<generatePackage>com.abc.XYZ.integration.test.jaxb</generatePackage>
<schemaDirectory>src\main\resources</schemaDirectory>
<generateDirectory>src\main\java</generateDirectory>
<removeOldOutput>true</removeOldOutput>
<includeSchemas>
<includeSchema>*.xsd</includeSchema>
</includeSchemas>
<includeBindings>
<includeBinding>*.xjb</includeBinding>
</includeBindings>
<strict>false</strict>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>



No comments: