Monday, May 10, 2010

Simple Web service using Spring WS 1.5.9

HOW-TO – Spring web service 1.5.9


This demo creates a web service using an XSD file

  1. Create a Dynamic web project in eclipse "SpringWSDemo1"
  2. Create the .xsd file (/SpringWSDemo1/resources/customer.xsd)

Note that the suffix should be mentioned as Request or Response viz: GetBankDetailsRequest, GetBankDetailsResponse

  1. Now using JAXB xjc compiler create the JAXB objects.

Refer http://www.xyzws.com/scdjws/SGS33/5 (\SpringWSDemo1\src\com\ats\pojo) It will contain package-info.java and ObjectFactory.java

  1. Create the service endpoint (\SpringWSDemo1\src\com\ats\webservice\CustomerInfoEndpoint.java)
  2. In web.xml configure MessageServletDispatcher

(\SpringWSDemo1\WebContent\WEB-INF\web.xml)

  1. Setup DefaultWsdl11Definition, PayloadRootAnnotationMethodEndpointMapping, MarshallingMethodEndpointAdapter, Jaxb2Marshaller in the spring context file (\SpringWSDemo1\WebContent\WEB-INF\spring-ws-servlet.xml)

6. Deploy and access WSDL at http://localhost:8090/SpringWSDemo1/custInfo.wsdl

Reference:

http://www.springbyexample.org/examples/simple-spring-web-services.html

http://static.springsource.org/spring-ws/sites/1.5/reference/html/tutorial.html


==================================================================

Files

===================================================================

1.

.classpath

---------


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.5.0_14">
<attributes>
<attribute name="owner.project.facets" value="jst.java"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/aopalliance-1.0.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/commons-logging-1.1.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/jaxen-1.1.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/jdom-1.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/log4j-1.2.15.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-aop-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-beans-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-context-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-context-support-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-core-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-oxm-1.5.9.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-web-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-webmvc-2.5.6.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-ws-core-1.5.9.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/spring-xml-1.5.9.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/stax-api-1.0-2.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/others/samples/tutorial/target/tutorial-1.5.9/WEB-INF/lib/wsdl4j-1.6.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/InstalledSoftwares/spring-ws-1.5.9/dist/spring-ws-1.5.9-all.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/InstalledSoftwares/spring-ws-1.5.9/lib/java-ee/activation-1.1.1.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-api.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-impl.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/InstalledSoftwares/spring-ws-1.5.9/lib/java-ee/saaj-api-1.3.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="C:/InstalledSoftwares/spring-ws-1.5.9/lib/saaj/saaj-impl-1.3.2.jar">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="build/classes"/>
</classpath>


===========================================================

2. customer.xsd

-------------


<?xml version="1.0" ?>
<xsd:schema targetNamespace="http://mycompany.com/customerInfo/definitions"
xmlns:tspace="http://mycompany.com/customerInfo/definitions" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.w3.org/2001/XMLSchema" />

<xsd:element name="GetBankDetailsRequest">
<xsd:complexType>
<xsd:all>
<xsd:element name="Customer" type="tspace:Customer" />
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetBankDetailsResponse">
<xsd:complexType>
<xsd:all>
<xsd:element name="BankDetails" type="tspace:BankDetails" />
</xsd:all>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="Customer">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"></xsd:element>
<xsd:element name="age" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BankDetails">
<xsd:sequence>
<xsd:element name="customer" type="tspace:Customer"></xsd:element>
<xsd:element name="accountNo" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

====================================================================

3.

CustomerInfoEndpoint.java

----------------------------


package com.ats.webservice;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.springframework.beans.BeanUtils;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import com.ats.pojo.BankDetails;
import com.ats.pojo.Customer;
import com.ats.pojo.GetBankDetailsRequest;
import com.ats.pojo.GetBankDetailsResponse;

@Endpoint
public class CustomerInfoEndpoint {
private final static QName _GetXResponse_QNAME = new QName("http://mycompany.com/customerInfo/definitions", "GetBankDetailsResponse");
private ManageCustomer manageCustomer;

public CustomerInfoEndpoint(ManageCustomer manageCustomer) {
this.manageCustomer = manageCustomer;
}

@PayloadRoot(localPart = "GetBankDetailsRequest", namespace = "http://mycompany.com/customerInfo/definitions")
public GetBankDetailsResponse getBankDetails(GetBankDetailsRequest request) {

BankDetails bankDetails = manageCustomer.retrieveInfo(request.getCustomer());
GetBankDetailsResponse response = new GetBankDetailsResponse();
response.setBankDetails(bankDetails);

return response;
}

}


===================================================================

4.

ManageCustomer.java

-----------------------


package com.ats.webservice;

import com.ats.pojo.BankDetails;
import com.ats.pojo.Customer;

public class ManageCustomer {


/**
* @param customer
* @return
*/
public BankDetails retrieveInfo(Customer customer) {

// You may replace this with call to Data layer
BankDetails bankDetails = new BankDetails();
bankDetails.setCustomer(customer);
bankDetails.setAccountNo(11);

return bankDetails;
}
}


================================================================

5.

web.xml

---------


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringWSDemo1</display-name>

<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


========================================================================

6.

spring-ws-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<bean id="custInfoEndpoint" class="com.ats.webservice.CustomerInfoEndpoint">
<constructor-arg ref="manageCustomer" />
</bean>

<bean id="manageCustomer" class="com.ats.webservice.ManageCustomer">
</bean>

<bean id="custInfo"
class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition"
p:portTypeName="CustomerInfo"
p:locationUri="http://localhost:8090/SpringWSDemo1">
<property name="schema">
<bean class="org.springframework.xml.xsd.SimpleXsdSchema" p:xsd="classpath:/customer.xsd" />
</property>
<property name="targetNamespace" value="http://mycompany.com/customerInfo/definitions"/>
</bean>

<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<description>An endpoint mapping strategy that looks for @Endpoint and
@PayloadRoot annotations.</description>
</bean>

<bean
class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
<description>Enables the MessageDispatchServlet to invoke methods
requiring OXM marshalling.</description>
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>

</bean>

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"
p:contextPath="com.ats.pojo" />

</beans>

========================================================================