Saturday, January 7, 2012

Develop JAX-WS based WebService in GlassFish

Here we are going to learn how to develop a Simple Jax-Ws webservice and deploy it in Glassfish,very manually.
Here I have developed this webservice as webApplication,then created a war file,and deployed it like a normal WebApplication

S/W Used:
jdk 1.6
GlassFish 3.0

Steps
-------


1.Prepare the directory structure











2.Code your resources
  
Calculator.java
----------------
package p1;
import javax.jws.WebService;
import javax.jws.WebMethod;

@WebService
public class Calculator
{
    @WebMethod
    public String sum(int x,int y)
    {
        return ""+(x+y);
    }
}


web.xml
----------
<web-app/>


3.Create your War file,using cmd
calculatorApp\Server> jar cf  MyCalculatorWs.war  .

4.Start your glassfish server
start-->All Program-->Sun Ms-->Application Server v2.1-->start Default Server

5.Deploy the created war file in Glassfish server.

copy MyCalculatorWs.war and paste it to
C:\Sun\AppServer\domains\domain1\autodeploy folder

After you deploy the above war file,you will get MyCalculatorWs.war_deployed file,which  means deployment is successful

6.View the WSDL File
http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL
                        ---- ----------------  ---------   ----------------
                       port  warfile Name   className      fixed word To access WSDL
__________________________________________________________________________
Server part is over,Now we need to write the client
Initially there is no file in client folder








1>Use wsimport tool to generate helper classes using which you can invoke webservice

client> wsimport -p mypack -keep http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL

-p mypack :create a package called mypack,and put all the generated java/class file in it
-keep     :preserve the java file,after .class is created

Note:
wsimport tool is built in tool available in jdk1.6(bin folder)

After the 1st step you will find the following







2.Add your JavaClient which calls the Calculator webservice using the above helperclasses
Add TestClient.java inside client folder(outside mypack folder)

TestClient.java
---------------
import mypack.Calculator;
import mypack.CalculatorService;

public class TestClient
{
public static void main (String [] args)
{
//locate webservice comp
CalculatorService cs=new CalculatorService();

//getAccess to B.obj ref of WebService comp
Calculator bobj=cs.getCalculatorPort();

//call B.methods
String result=bobj.sum(5,10);
System.out.println("The sum is"+result);
}
}

4.Run & Execute TestClient.java
 Client>javac *.java
 Client>java TestClient

Download Source here

No comments:

Post a Comment