Showing posts with label webservice. Show all posts
Showing posts with label webservice. Show all posts

Tuesday, November 16, 2010

Create a Hello World Webservice Using Axis2


Steps involved in creating Hello World Webservice using axis2
Steps :
1.       Develop a service class(the one you are going to expose as a service)
2.       Write a service descriptor e.g  services.xml
3.       Compile the service class and create web services archieve file
4.       Deploy into axis server(copy the aar file into services folder).


Step 1:      Create the Service class


Package vels.test.ws;

public class HelloWorldService 
{
  public String sayHello(String name) {
    
      out.println("Hello World Service called");
    return "Hello : " + name;
  }
}


Step 2 : Write a Webservice descripttor (service configuration file)

In Axis2 the service configuration (descriptor) file used is services.xml.
The services.xml file must present in the META-INF directory of the service achieve.

services.xml  :
  <service>
 <parameter name="ServiceClass" locked="false">  
                      vels.test.ws.HelloWorldService</parameter>
 <operation name="sayHello">
 <messageReceiver 
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>


                 Here  our  service class is vels.test.ws.HelloWorldService  and
                 the message receiver is org.apache.axis2.rpc.receivers.RPCMessageReceiver.
                 The operation we are exposing is sayHello.

Step 3 :  Creating the archive file for the deployment


  •   Compile all the java files and make it as jar(aar)
  •     For example you have placed all the class files into the following  folder D:\dev\HelloWorld\bin
  •    Run the following command(before that set the java class path )D:\dev\HelloWorld\bin>jar cvf HelloWorldService.aar *(it will take all  the class files and services.xml files and make it as HelloWorldService.aar file)

Deploy the Webservice :    


Creating Client program to access the deployed webservice :

1.   Generate the client side java class From WSDL using axis  WSDLToJava     This will create client specific java files
      
        Place all these jars into class path
        set CLASSPATH=.;D:\lib\axis\axis-1.3.jar;
                   D:\lib\axis\commons-logging-1.0.4.jar;
                   D:\lib\axis\commons-discovery-0.2.jar;
                   D:\lib\axis\jaxrpc.jar;
                   D:\lib\axis\log4j-1.2.8.jar;
                   D:\lib\axis\wsdl4j-1.5.1.jar;D:\lib\axis\saaj.jar;

     java org.apache.axis.wsdl.WSDL2Java ..\res\wsdl\Hello.wsdl -o ..\src –W


2. write a client to test the service

public class Test {
    public static void main(String[] args) throws Exception {
      HelloSOAP11BindingStub stub = new HelloSOAP11BindingStub(new java.net.URL("http://localhost:8080/axis2/services/hello"),null);
        //Create the request
       
       SayHello  request = new
  SayHello();

       request.setName("vels");
        //Invoke the service
        SayHelloResponse response
           = stub.sayHello(request);
       System.out.println("Response : " + response.get_return());
   }
}


Hope u guys got come idea abt webservices.........

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