Peter Arockiaraj

Can you hear me now?…Good!

Developing Web Services By Using Spring and CXF


Step 1:  Download Following Jar files.

Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.

commons-logging-1.1.jar

geronimo-activation_1.1_spec-1.0-M1.jar (or Sun’s Activation jar)

geronimo-annotation_1.0_spec-1.1.jar (JSR 250)

geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun’s JavaMail jar)

geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun’s Servlet jar)

geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)

jaxb-api-2.0.jar

jaxb-impl-2.0.5.jar

jaxws-api-2.0.jar

neethi-2.0.jar

saaj-api-1.3.jar

saaj-impl-1.3.jar

stax-api-1.0.1.jar

wsdl4j-1.6.1.jar

wstx-asl-3.2.1.jar

XmlSchema-1.2.jar

xml-resolver-1.2.jar

The Spring jars:

aopalliance-1.0.jar

spring-core-2.0.4.jar

spring-beans-2.0.4.jar

spring-context-2.0.4.jar

spring-web-2.0.4.jar

And the CXF jar:

cxf-2.0-incubator.jar

Step 2:  Create Following Folder Structure

Folder Structure

Step 3:  Create Following classes

  1. Address
  2. AddressType
  3. IAddressService
  4. AddressService

Address.java

package com.sungard.adms.address.ws;

public class Address {

private String name;

private String doorno;

private String road;

private String buildingName;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getRoad() {

return road;

}

public void setRoad(String road) {

this.road = road;

}

public String getBuildingName() {

return buildingName;

}

public void setBuildingName(String buildingName) {

this.buildingName = buildingName;

}

public String getDoorno() {

return doorno;

}

public void setDoorno(String doorno) {

this.doorno = doorno;

}

}

AddressType.java

package com.sungard.adms.address.ws;

public enum AddressType {

HEBBALOFFICE,

ELECTRONIC_CITY,

ESTEEM_TOWERS,

LANGFORD_ROAD

}

IAddressService.java

package com.sungard.adms.address.ws;

/**

* CXF sample interface.

*

*/

@WebService

public interface IAddressService {

public Address getAddress(AddressType adsType);

}

AddressService.java

package com.sungard.adms.address.ws;

import java.util.HashMap;

/**

* This is the Implementation Class for IAddressService. Our Business Logic will

* be present here.

*

* @author PA71448

*

*/

@WebService(endpointInterface = “com.sungard.adms.address.ws.IAddressService”)

public class AddressService implements IAddressService {

private HashMap<AddressType, Address> addressList = new HashMap<AddressType, Address>();

/**

* Default Constructor for this service.

*/

public AddressService() {

Address bhr = new Address();

bhr.setBuildingName(“Kirloskar Business Park”);

bhr.setName(“Sungard”);

bhr.setRoad(“Hyderabad Road”);

bhr.setDoorno(“338”);

addressList.put(AddressType.HEBBALOFFICE, bhr);

Address bsd = new Address();

bsd.setBuildingName(“Electronic City”);

bsd.setName(“Sungard”);

bsd.setRoad(“Hosur Road”);

bsd.setDoorno(“143”);

addressList.put(AddressType.ELECTRONIC_CITY, bsd);

Address bet = new Address();

bet.setBuildingName(“Esteem Towers”);

bet.setName(“Sungard”);

bet.setRoad(“Railway Parallel Road”);

bet.setDoorno(“6”);

addressList.put(AddressType.ESTEEM_TOWERS, bet);

Address bla = new Address();

bla.setBuildingName(“LangFord Avenue”);

bla.setName(“Sungard”);

bla.setRoad(“LangFord Road”);

bla.setDoorno(“653”);

addressList.put(AddressType.LANGFORD_ROAD, bla);

}

@Override

public Address getAddress(AddressType adsType) {

return addressList.get(adsType);

}

}

Step 4:  Create beans.xml file in WEB-INF folder

beans.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;

xmlns:jaxws=”http://cxf.apache.org/jaxws&#8221;

xsi:schemaLocation=”

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd”&gt;

<import resource=”classpath:META-INF/cxf/cxf.xml” />

<import resource=”classpath:META-INF/cxf/cxf-extension-soap.xml” />

<import resource=”classpath:META-INF/cxf/cxf-servlet.xml” />

<jaxws:endpoint

id=”helloWorld”

implementor=”com.sungard.adms.address.ws.AddressService”

address=”/AddressService” />

</beans>

Step 5:  Create web.xml file in WEB-INF folder

web.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<!DOCTYPE web-app

PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”

http://java.sun.com/dtd/web-app_2_3.dtd”&gt;

<web-app>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>WEB-INF/beans.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<servlet>

<servlet-name>CXFServlet</servlet-name>

<display-name>CXF Servlet</display-name>

<servlet-class>

org.apache.cxf.transport.servlet.CXFServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>CXFServlet</servlet-name>

<url-pattern>/*</url-pattern>

</servlet-mapping>

</web-app>

Step 6:  Copy All class files with folder structure in WEB-INF classes Folder

Step 7:

Create ant directory in project root folder

Create build.xml file

build.xml

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

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

<target name=”archive”>

<jar destfile=”cxfaddressservice.war”  >

<fileset dir=”${basedir}”>

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

</fileset>

<fileset dir=”${basedir}”>

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

</fileset>

<fileset dir=”${basedir}”>

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

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

</fileset>

</jar>

</target>

</project>

Step 8: Run ant build

Step 9: Deploy the war file in application server/web server

Step 10: 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/cxfaddressservice/AddressService?wsdl

WSDL file

Developing Web Services Client by Using JWSDP

Step 1: Download Following Jar files.

jaxrpc-impl.jar

jaxrpc-spi.jar

activation.jar

mail.jar

dom.jar

sax.jar

xalan.jar

xercesImpl.jar

saaj-impl.jar

FastInfoset.jar

saajImpl.jar

jsr173_api.jar

Step 2: Create a j2EE project AddressWSClient

Step 3: Create folders src, classes, lib ,wsdl under project root folder.

Step 4: Create file config.xml inside the project root as

config.xml

<?xml version=”1.0″ encoding=

“UTF-8″?>

<configuration xmlns=”http://java.sun.com/xml/ns/jax-rpc/ri/config”&gt;

<wsdl location=

“wsdl\AddressService.wsdl”

packageName=

“com.sungard.adms.address.client.gen” />

</configuration>

Step 5: Copy the .wsdl from http://localhost:8080/cxfaddressservice/AddressService?wsdl into AddressWSClient/wsdl

Step 6: Generate client side stubs by executing…

wscompile -verbose -gen -d classes -s src -keep config.xml

Step 7: Copy all jar file into lib (Add them to build path)

Step 8: Create class AddressClient.java as

AddressClient.java

package com.sungard.adms.address.client.gen;

import java.net.MalformedURLException;

import java.net.URL;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import javax.xml.rpc.Service;

import javax.xml.rpc.ServiceException;

import javax.xml.rpc.ServiceFactory;

/**

* @author Peter Arockiaraj

*

* TODO To change the template for this generated type comment go to

* Window – Preferences – Java – Code Style – Code Templates

*/

public class AddressClient {

public static void main(String[] args) throws ServiceException,RemoteException,MalformedURLException{

try{

System.setProperty(“javax.xml.soap.MessageFactory”, “com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl”);

//System.setProperty(“javax.xml.soap.SOAPFactory”,”com.sun.xml.messaging.saaj.soap.ver1_1.SOAPFactory1_1Impl”);

//System.setProperty(“javax.xml.rpc.ServiceFactory”,”com.sun.xml.rpc.client.ServiceFactoryImpl”);

//System.setProperty(“javax.xml.soap.SOAPConnectionFactory”,”javax.xml.soap.SOAPConnectionFactory”);

//System.setProperty(“javax.xml.parsers.DocumentBuilderFactory”,”com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl”);

AddressServiceService_Impl impl =

new AddressServiceService_Impl();

IAddressService ws = impl.getAddressServicePort();

Address result = ws.getAddress(AddressType.HEBBALOFFICE);

System.out.println(“Direct Call Output\n”);

System.out.println(“Company Name:\t”+result.getName());

System.out.println(“Door No:\t”+result.getDoorno());

System.out.println(“Building Name:\t”+result.getBuildingName());

System.out.println(“Road Name:\t”+result.getRoad());

System.out.println(“Dynamic WS call”);

URL url = new URL(“http://localhost:8080/cxfaddressservice/AddressService?wsdl&#8221;);

QName qname = new QName(“http://ws.address.adms.sungard.com/&#8221;,”AddressServiceService”);

ServiceFactory factory = ServiceFactory.newInstance();

Service service = factory.createService(url,qname);

IAddressService hello = (IAddressService) service.getPort(IAddressService.class);

Address dynaddress=ws.getAddress(AddressType.ESTEEM_TOWERS);

System.out.println(“\nDynamic Call Output\n”);

System.out.println(“Company Name:\t”+dynaddress.getName());

System.out.println(“Door No:\t”+dynaddress.getDoorno());

System.out.println(“Building Name:\t”+dynaddress.getBuildingName());

System.out.println(“Road Name:\t”+dynaddress.getRoad());

}catch(Exception se){

System.out.println(“ServiceException ” + se.getMessage());

}

}

}

Step 9: Run AddressClient.java as a normal java application.

Client Output

Developing Simple Web Services Client by Using Flex

Step 1: Create New Flex Project in Flex Builder.

Step 2:  Open Main Application MXML.

Step 3: Copy & Paste Following Code.

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

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; xmlns=”*” layout=”absolute”

creationComplete=”userRequest.returnRecords()” height=”249″ width=”538″>

<mx:Form x=”22″ y=”10″ width=”493″>

<mx:HBox>

<mx:Label text=”Address Code”/>

<mx:TextInputfont-size:10.0pt;font-family:”Courier New”;mso-fareast-font-family:”Times New Roman”; color:#990000″>addresscode”/>

</mx:HBox>

<mx:HBox>

<mx:DataGridfont-size:10.0pt;font-family:”Courier New”; mso-fareast-font-family:”Times New Roman”;color:#990000″>dgUserRequest” x=”22″ y=”128″>

<mx:columns>

<mx:DataGridColumn headerText=”Building Name” dataField=”buildingName”/>

<mx:DataGridColumn headerText=”Door Number” dataField=”doorno”/>

<mx:DataGridColumn headerText=”Name” dataField=”name”/>

<mx:DataGridColumn headerText=”Road Name” dataField=”road”/>

</mx:columns>

</mx:DataGrid>

</mx:HBox>

<mx:Button label=”Submit” click=”clickHandler()”/>

</mx:Form>

<mx:WebService

id=”userRequest”

wsdl=”http://localhost:8080/cxfaddressservice/AddressService?wsdl”&gt;

<mx:operation name=”getAddress” resultFormat=”object” result=”insertCFCHandler(event)”

fault=”mx.controls.Alert.show(event.fault.faultString)”/>

</mx:WebService>

<mx:Script>

<![CDATA[

import mx.rpc.events.ResultEvent;

private function insertCFCHandler(e:ResultEvent):void

{

dgUserRequest.dataProvider = e.result;

}

private function clickHandler():void

{

userRequest.getAddress(addresscode.text);

}

]]>

</mx:Script>

</mx:Application>

Step 4:  Run Application.

FlexOutput

September 2, 2009 - Posted by | Web Services

No comments yet.

Leave a comment