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 :

1.Jdk1.6
2.Jboss 4.2.2GA
3.Ant
4.Eclipse

Step1 :

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

package com.vels.test.ws;

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

@WebService
public class HelloJaxWs {

@WebMethod
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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


           <servlet>
                     <servlet-name>helloJaxWS</servlet-name>
                    <servlet-class>com.vels.test.ws.HelloJaxWs</servlet-class>
             </servlet>

            <servlet-mapping>
                       <servlet-name>helloJaxWS</servlet-name>
                        <url-pattern>/helloJaxWS</url-pattern>
           </servlet-mapping>
</web-app>

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="jsp.dir.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"/>
</war>
<copy file="${WAR_FILE}" todir="${WS_DEPLOY_DIR}" overwrite="true" />
</target>

</project>



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.....http://www.blogger.com/=



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="web.services.classpath">
<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" />
</fileset>

</path>
</target>

<target name="wstaskdef">
<taskdef name="wsconsume"
classname="org.jboss.wsf.spi.tools.ant.WSConsumeTask">
<classpath>
<path refid="web.services.classpath" />
</classpath>
</taskdef>
</target>

<target name="wsdltojava" depends="init,wstaskdef">
  <wsconsume fork="true"
    verbose="true"
   sourcedestdir="../src"
      keep="true" package="com.vels.ws.test.wsclient"
   wsdl="http://127.0.0.1:8080/HelloJaxWs/helloJaxWS?wsdl"
/>
</target>
</project>



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


Step 2: Write the Client to access the exposed service


package com.vels.ws.test.wsclient;

import javax.xml.ws.BindingProvider;

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

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

} catch (Throwable e){
e.printStackTrace();
}

}
}


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