Monday, November 15, 2010

How to create JaxWs Webservice with ant and Jboss4.2.2

Simple Steps To create JaxWs Webservice using JBOSS 4.2.2

Pre- Requirement :

2.Jboss 4.2.2GA

Step1 :

Write the service which is going to be exposed as a webservice


import javax.jws.WebMethod;
import javax.jws.WebService;

public class HelloJaxWs {

public String sayHello(String message) {
System.out.println("sayHello:" + message);
return "You said '" + message + "'";

Step 2: Create the deployment descriptor (web.xml)

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">



Step 3:

Compile the java source and place class file in to /WebContent/WEB-INF/classes

Step 4:  
               Run the Build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="deploy">
<property name="" value="myapp" />

<property name="WAR_FILE" value="HelloJaxWs.war" />
<property name="JBOSS_HOME" value="D:\apps\jboss\jboss-4.2.2" />
<property name="WS_DEPLOY_DIR" value="${JBOSS_HOME}\server\default\deploy" />

<target name="deploy" description="war file creation">
<war destfile="${WAR_FILE}" webxml="../WebContent/WEB-INF/web.xml">
<classes dir="../WebContent/WEB-INF/classes"/>
<copy file="${WAR_FILE}" todir="${WS_DEPLOY_DIR}" overwrite="true" />


This script will create the War file and deploy into the server deploy folder

Your service is published on the server and u can get the WSDL URL.....

Creating a Client to acces the exposed service
Step 1: Write the ant script to create the client spesific Java classes

<?xml version="1.0" encoding="UTF-8"?>
<project name="project" default="wsdltojava">

<property name="JAVA_HOME" value="D:\tools\Java\jdk1.6.0_05" />
<property name="JBOSS_HOME" value="D:\apps\jboss\jboss-4.2.2" />

<target name="init">
<path id="">
<fileset dir="${JAVA_HOME}\lib" includes="*.jar" />
<fileset dir="${JBOSS_HOME}\lib\endorsed\" includes="*.jar" />
<fileset dir="${JBOSS_HOME}\lib\" includes="*.jar" />
<fileset dir="${JBOSS_HOME}\client">
<include name="activation.jar" />
<include name="getopt.jar" />
<include name="wstx.jar" />
<include name="jbossall-client.jar" />
<include name="log4j.jar" />
<include name="mail.jar" />
<include name="jbossws-spi.jar" />
<include name="stax-api.jar" />
<include name="jaxb-api.jar" />
<include name="jaxb-impl.jar" />
<include name="jaxb-xjc.jar" />
<include name="streambuffer.jar" />
<include name="stax-ex.jar" />
<include name="javassist.jar" />
<include name="jboss-xml-inding.jar" />
<include name="jbossws-client.jar" />
<include name="jboss-jaxws.jar" />
<include name="jboss-jaxrpc.jar" />
<include name="jboss-saaj.jar" />
<include name="jboss-srp-client.jar" />
<include name="jbossws-common.jar" />
<include name="jaxws-tools.jar" />
<include name="jaxws-rt.jar" />


<target name="wstaskdef">
<taskdef name="wsconsume"
<path refid="" />

<target name="wsdltojava" depends="init,wstaskdef">
  <wsconsume fork="true"
      keep="true" package=""

It will create all client specific java files and skeletons using WsConsume tool.

Step 2: Write the Client to access the exposed service



public class TestClient {
public static void main (String[] a){
System.out.println(" before execution..........");
HelloJaxWsService ws= new HelloJaxWsService();

System.out.println(" before calling
HelloJaxWs port = (HelloJaxWs)ws.getHelloJaxWsPort();
System.out.println(port.sayHello(" good evening"));

} catch (Throwable e){


Output : You said good evening

I will work on some other webservice samples get back to u guys.........c u ....

No comments:

Post a Comment