Peter Arockiaraj

Can you hear me now?…Good!

Developing Web services Using Spring Framework


Required Softwares:

  1. JAXB
  2. Tomcat Server

Step 1: Download Following Jar files.

Open up your favorite IDE and create a new project StudentWS. The first thing we need to do is add the necessary Spring dependencies to the project.

acegi-security-1.0.6.jar

activation.jar

activation-1.1.1.jar

ant-dotnet-1.0.jar

axiom-api-1.2.8.jar

axiom-impl-1.2.8.jar

bcel-5.1.jar

bcprov-jdk14-140.jar

castor-1.2.jar

commons-collections-3.2.jar

commons-httpclient-3.1.jar

commons-io-1.3.1.jar

commons-lang-2.1.jar

commons-logging-1.1.1.jar

dom4j-1.6.1.jar

isorelax-20030108.jar

jaxb1-impl.jar

jaxb-api-2.1.jar

jaxb-impl-2.1.5.jar

jaxb-libs-1.0.6.jar

jaxb-xjc.jar

jaxen-1.1.1.jar

jchardet-1.0.jar

jdom-1.0.jar

jibx-bind-1.1.6a.jar

jibx-run-1.1.6a.jar

jms-1.1.jar

jsr173_1.0_api.jar

junit-4.4.jar

log4j-1.2.15.jar

mail-1.4.1.jar

maven-ant-tasks-2.0.7.jar

opensaml-1.1.jar

oro-2.0.8.jar

plexus-utils-1.1.jar

plexus-velocity-1.1.3.jar

relaxngDatatype-20020414.jar

saaj-api-1.3.jar

saaj-impl-1.3.2.jar

servlet-api-2.5.jar

slf4j-api-1.3.1.jar

spring-aop-2.5.6.jar

spring-beans-2.5.6.jar

spring-context-2.5.6.jar

spring-core-2.5.6.jar

spring-jdbc-2.5.6.jar

spring-jms-2.5.6.jar

spring-orm-2.5.6.jar

spring-oxm-1.5.6.jar

spring-oxm-tiger-1.5.6.jar

spring-security-core-2.0.2.jar

spring-test.jar

spring-tx-2.5.6.jar

spring-web-2.5.6.jar

spring-webmvc-2.5.6.jar

spring-ws-1.5.6-all.jar

spring-ws-1.5.6-sources.jar

spring-ws-archetype-1.5.4.jar

spring-ws-core-1.5.6.jar

spring-ws-core-tiger-1.5.6.jar

spring-ws-security-1.5.6.jar

spring-ws-support-1.5.6.jar

spring-xml-1.5.6.jar

stax-api-1.0.1.jar

velocity-1.5.jar

velocity-dep-1.4.jar

wsdl4j-1.6.1.jar

wss4j-1.5.5.jar

wstx-asl-3.2.7.jar

xalan-2.7.0.jar

xbean-2.2.0.jar

xercesImpl-2.8.1.jar

xml-apis-1.3.04.jar

XmlSchema-1.4.3.jar

xmlsec-2.0.jar

xom-1.1.jar

xpp3-1.1.3.4.O.jar

xsdlib-20060615.jar

xstream-1.3.jar

xws-security-2.0-FCS.jar

Step 2: Create Following Folder Structure

Spring Folder Structure

Step 3: Create .xsd file to design your request and response objects.

studentregistration.xsd

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>

<xs:schema xmlns=”http://studentregistration.banner.sungardhe.com&#8221;

xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221;

targetNamespace=”http://studentregistration.banner.sungardhe.com&#8221;

elementFormDefault=”unqualified” attributeFormDefault=”unqualified”>

<xs:element name=”StudentRegistrationRequest”>

<xs:complexType>

<xs:sequence minOccurs=”1″>

<xs:element ref=”University” />

<xs:element ref=”Registrant” />

</xs:sequence>

<xs:attribute name=”version” type=”xs:string”

use=”optional” />

</xs:complexType>

</xs:element>

<xs:element name=”StudentRegistrationResponse”>

<xs:complexType>

<xs:sequence>

<xs:element ref=”RegistrationCode” />

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name=”University”>

<xs:complexType>

<xs:choice>

<xs:element ref=”UniversityId” />

<xs:element ref=”UniversityName” />

<xs:element ref=”CourseName” />

</xs:choice>

</xs:complexType>

</xs:element>

<xs:element name=”UniversityName”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”25″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”CourseName”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”25″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”UniversityId”>

<xs:simpleType>

<xs:restriction base=”xs:int” />

</xs:simpleType>

</xs:element>

<xs:element name=”Registrant”>

<xs:complexType>

<xs:sequence>

<xs:element ref=”FirstName” />

<xs:element minOccurs=”0″ maxOccurs=”1″

ref=”MiddleName” />

<xs:element ref=”LastName” />

<xs:element ref=”Email” />

<xs:element ref=”Phone” />

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name=”FirstName”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”25″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”MiddleName”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”15″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”LastName”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”75″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”Email”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”100″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”Phone”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”100″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

<xs:element name=”RegistrationCode”>

<xs:simpleType>

<xs:restriction base=”xs:string”>

<xs:maxLength value=”50″ />

</xs:restriction>

</xs:simpleType>

</xs:element>

</xs:schema>

Step 4: Create .xjb file for customizing your JAXB Bindings.

For More Information: http://java.sun.com/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

studentregistration.xjb

<jxb:bindings version=”1.0″ xmlns:jxb=”http://java.sun.com/xml/ns/jaxb&#8221;

xmlns:xjc=”http://java.sun.com/xml/ns/jaxb/xjc&#8221;

xmlns:xs=”http://www.w3.org/2001/XMLSchema”&gt;

<jxb:bindings schemaLocation=”studentregistration.xsd” node=”/xs:schema”>

<jxb:schemaBindings>

<jxb:package name=”com.sungardhe.banner.studentregistration.jaxb” />

</jxb:schemaBindings>

</jxb:bindings>

</jxb:bindings>

Step 5: Create new folder ‘resources’ Under src folder.

Step 6: Copy .xsd and .xjb files into resource Directory.

Step 7: compile .xsd file using xjc compiler from src folder.

xjc resources\studentregistration.xsd -b resources\studentregistration.xjb

XJC Output

Step 8: Create following Interface and class under com.sungardhe.banner.ws.student Package.

  1. MarshallingStudentService
  2. StudentEndpoint

MarshallingStudentService.java

package com.sungardhe.banner.ws.student;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationRequest;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationResponse;

/**

*

* @author PeterA

*

*/

public interface MarshallingStudentService {

/**

* NameSpace URL

*/

public final static String NAMESPACE = “http://studentregistration.banner.sungardhe.com&#8221;;

/**

* Request Object Element Name.

*/

public final static String GET_PERSONS_REQUEST = “StudentRegistrationRequest”;

/**

* Gets Student Request.

*

* @param request

* @return

*/

public StudentRegistrationResponse getPersons(

StudentRegistrationRequest request);

}

StudentEndpoint.java

package com.sungardhe.banner.ws.student;

import org.springframework.ws.server.endpoint.annotation.Endpoint;

import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import com.sungardhe.banner.studentregistration.jaxb.ObjectFactory;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationRequest;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationResponse;

/**

*

* @author PeterA

*

*/

@Endpoint

public class StudentEndpoint implements MarshallingStudentService {

/**

* Gets Student Request from web services client.

*

* @param request

* @return

*/

@PayloadRoot(localPart = GET_PERSONS_REQUEST, namespace = NAMESPACE)

public StudentRegistrationResponse getPersons(

StudentRegistrationRequest request) {

/* Printing Requested Student name. */

System.out.println(“Requested Student Name::”

+ request.getRegistrant().getFirstName() + ” ”

+ request.getRegistrant().getMiddleName() + ” ”

+ request.getRegistrant().getLastName());

/* Printing Requested Student E-Mail. */

System.out.println(“Requested Student Email::”

+ request.getRegistrant().getEmail());

/* Printing Requested Student Phone. */

System.out.println(“Requested Student Phone::”

+ request.getRegistrant().getPhone());

/* Printing Requested Student Applied University ID. */

System.out.println(“Applied University ID::”

+ request.getUniversity().getUniversityId());

/* Printing Requested Student Applied University Name. */

System.out.println(“Applied University Name::”

+ request.getUniversity().getUniversityName());

/* Printing Requested Student Applied Course Name. */

System.out.println(“Applied Course Name::”

+ request.getUniversity().getCourseName());

/* Creating ObjectFactory Object for creating response object. */

ObjectFactory objectFactory = new ObjectFactory();

/*

* Creating Response Object.

*/

StudentRegistrationResponse response = objectFactory

.createStudentRegistrationResponse();

/*

* Populating Response Object.

*/

response

.setRegistrationCode(“Student Registration Done: Registration Code:: SR567432”);

/*

* Returning Response Object.

*/

return response;

}

}

Step 9: Create spring-ws-servlet.xml file under WEB-INF folder.

spring-ws-servlet.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd”&gt;

<bean />

<bean id=”marshaller”

>

<property name=”contextPath”

value=”com.sungardhe.banner.studentregistration.jaxb” />

</bean>

<bean id=”unmarshaller”

>

<property name=”contextPath”

value=”com.sungardhe.banner.studentregistration.jaxb” />

</bean>

<bean id=”service”

>

<property name=”builder”>

<bean

>

<property name=”schema”

value=”/WEB-INF/classes/resources/studentregistration.xsd” />

<property name=”portTypeName” value=”services” />

<property name=”locationUri”

value=”http://localhost:8080/StudentWS/&#8221; />

<property name=”targetNamespace”

value=”http://studentregistration.banner.sungardhe.com&#8221; />

</bean>

</property>

</bean>

<bean

>

<description>

An endpoint mapping strategy that looks for @Endpoint and

@PayloadRoot annotations.

</description>

</bean>

<bean

>

<description>

Enables the MessageDispatchServlet to invoke methods

requiring OXM marshalling.

</description>

<constructor-arg ref=”marshaller” />

</bean>

</beans>

Step 10: Create web.xml file under WEB-INF Folder.

web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&#8221;

version=”2.4″>

<display-name>Archetype Created Web Application</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>

</web-app>

Step 11: Create build.xml file under ant folder.

build.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”ws” basedir=”../” default=”archive”>

<target name=”archive”>

<jar destfile=”StudentWS.war”>

<fileset dir=”${basedir}”>

<include name=”**/*.class” />

</fileset>

<fileset dir=”${basedir}”>

<include name=”**/*.jar” />

</fileset>

<fileset dir=”${basedir}”>

<include name=”**/*.xml” />

<include name=”**/*.xsd” />

<include name=”**/*.xjb” />

<exclude name=”**/*build*” />

</fileset>

</jar>

</target>

</project>

Step 12: run build.xml using ant to generate StudentWS.war file.

Step 13: Run Tomcat server.

Step 14: Deploy StudentWS.war file into $TomcatHome/webapps folder.

Step 15: Check application deployed properly or not by using following url. This url will show the wsdl file for this web service application.

http://localhost:8080/StudentWS/service.wsdl

WSDL file

Developing Simple Web Services Client by Using Sping

Step 1: Create Following class files.

  1. StudentClient.java
  2. ApplicationClient.java

StudentClient.java

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

import com.sungardhe.banner.studentregistration.jaxb.Registrant;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationRequest;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationResponse;

import com.sungardhe.banner.studentregistration.jaxb.University;

public class StudentClient {

private WebServiceTemplate webServiceTemplate;

public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {

this.webServiceTemplate = webServiceTemplate;

}

public StudentRegistrationResponse send() {

StudentRegistrationRequest request = new StudentRegistrationRequest();

Registrant registrant = new Registrant();

registrant.setFirstName(“Jesus”);

registrant.setLastName(“Chirst”);

registrant.setMiddleName(“Joseph”);

registrant.setEmail(“Jesus@god.com”);

registrant.setPhone(“9876543210”);

University university = new University();

university.setUniversityId(12345);

university.setUniversityName(“Holy University”);

university.setCourseName(“Service”);

request.setRegistrant(registrant);

request.setUniversity(university);

request.setVersion(“1.0”);

return ((StudentRegistrationResponse) webServiceTemplate

.marshalSendAndReceive(request));

}

}

ApplicationClient.java

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationResponse;

public class ApplicationClient {

public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(

“applicationContext.xml”);

StudentClient mc = (StudentClient) ctx.getBean(“studentclient”);

StudentRegistrationResponse obj = mc.send();

System.out.println(obj.getRegistrationCode());

}

}

Step 2: Create applicationContext.xml file and place this file in classpath.

applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”&gt;

<bean id=”studentclient” class=”StudentClient”>

<property name=”webServiceTemplate” ref=”webServiceTemplate” />

</bean>

<bean id=”messageFactory”

/>

<bean id=”webServiceTemplate”

>

<constructor-arg ref=”messageFactory” />

<property name=”marshaller” ref=”marshaller” />

<property name=”unmarshaller” ref=”marshaller” />

<property name=”defaultUri”

value=”http://localhost:8080/StudentWS&#8221; />

</bean>

<bean id=”marshaller”

>

<property name=”classesToBeBound”>

<list>

<value>

com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationRequest

</value>

<value>

com.sungardhe.banner.studentregistration.jaxb.StudentRegistrationResponse

</value>

</list>

</property>

</bean>

</beans>

Step 3: Run ApplicationClient.java as a normal java application.

Client Output

Step 4: Check server console it displays Request Object.

Server Console Output

September 2, 2009 - Posted by | Web Services

4 Comments »

  1. Have you ever considered about adding a little bit more than just
    your articles? I mean, what you say is fundamental and all.
    Nevertheless think about if you added some great photos
    or videos to give your posts more, “pop”! Your content is excellent but with pics and clips, this blog could definitely be one of the greatest in its niche.
    Very good blog!

    Comment by hotmail support | April 19, 2013 | Reply

  2. Good way of telling, and fastidious post to
    get information about my presentation topic, which i am going to present in school.

    Comment by abrir cuenta facebook | May 8, 2013 | Reply

  3. Hello, after reading this awesome piece of writing i am as well delighted to share my knowledge here
    with colleagues.

    Comment by http://isidora.bligoo.com/content/view/1017869/sii-publica-informacion-falsa.html | May 8, 2013 | Reply

  4. Hey! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no back up.
    Do you have any solutions to protect against hackers?

    Comment by Clark | June 15, 2013 | Reply


Leave a comment