JBossWS example.

Jboss WS(http://jboss.org/jbossws) is a web service framework provided on Jboss Application Server to develop JAX WS compliant WebService. Developing a webservice is a very simple procee. Here I will try to develop a webservice and show how can we access this web service.

We can develop a webservice eith by using Servlet Container Model or by EJB container model. Here I use servlet cotainer model to develop a web service.

Here is our simple business method:-

package com.vivek.hello;
public class Hello{
public String sayHello(String userName){
return "Hello "+ userName;
}
}
}
Exposing this business method is bery simple. We just need to add some annotations and package it as a war file.

Annotations:-
1.javax.jws.WebService :– This annotation is placed at class level and this tells that the class is a web service.
2.javax.jws.WebMethod :– This annotation is placed at method level and this tells that this method is exposed as Wbservice.

After applying these 2 annotation our class looks like:-

package com.vivek.hello;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class Hello{
@WebMethod
public String sayHello(String userName){
return "Hello "+ userName;
}
}
}
Now we have to write deployment descriptor for this webservice. This is same like writing deployment descriptor for a servlet.

Web.xml content:-

<?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">
<display-name>JbossWS</display-name>
<servlet>
<servlet-name>HelloWebService</servlet-name>
<servlet-class>com.vivek.hello.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWebService</servlet-name>
<url-pattern>/Hello/*</url-pattern>
</servlet-mapping>
</web-app>


Packaging:-

Create a war file with the above class and web.xml.

That’s it. Here we have developed a simple web service. Place this war file in
folder and start the Jobss server.

To check this WebService open a browser and put http://localhost:8080//Hello/?wsdl. Ie( http://localhost:8080/JbossWs/Hello/?wsdl) If you get wsdl description then this web service is deployed successfully.

Here is the output of the browser:-

Accessing webservice:-
There are three methods to access a webservice:-
1.Stub.
2.Dynamic Proxy
3.Dymaic Invocation Interface(DII):-

Stub is very simple, here we use the wsdl to generate different stub and skelton classes usind WSDL to Java conversion tool(http://ws.apache.org/axis/java/user-guide.html#UsingWSDLWithAxis ) and use these classes to access the web service like any standalone java program.

DII and Dynamic Proxy are more flexiable. Here we generate the stub skelton in runtime. These two differ int the way we access the webservice. Let me demonstrate this by an example:-

Dynamic Invocation Interface:-

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class Test {
public static void main(String[] argv)throws Exception
{
try
{
ServiceFactory factory = ServiceFactory.newInstance();
//Lookup Service. . Service is entry point for a web service.
Service service = factory.createService(new URL(
"http://localhost:8080/JbossWS/?wsdl"), new QName(
"http://hello.vivek.com/", "HelloService"));
//A webservice can have di,fferent port. Port is like a library of methods.
//Set the location of wsdl.
Call call = service.createCall(new QName("http://hello.vivek.com/","HelloPort" ),new QName("http://hello.vivek.com/","sayHello"));
//Register input parameter and return type.

Object[] params = {"Vivek"};
//Invoke the method.
String result = (String)call.invoke(params);
//Print the result.
System.out.println(result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Output:-

Here we execute the method using method name. This is like reflection API. Once we know the details we can call the methods in runtime. This is more flexible than Stub as we dont need to complie the calsses if out webservice interface changes.

Hello Vivek

Note:- Here I used AXIS to call web service.

Dynamic Proxy:-

Dynamic proxy is less flexiable than DII and more flexiable than Dtub. Here we dynamically look up webservice interface.

Letus first create a new webservice with interface:-

Business Interface:-

package com.vivek.hello.common;

import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.jws.WebService;

@WebService
public interface IHello extends Remote{
public String sayHello(String userName) throws RemoteException;
}

Webservice Inplementation:-

package com.vivek.hello;

import javax.jws.WebMethod;
import javax.jws.WebService;
import com.vivek.hello.common.IHello;

@WebService(endpointInterface="com.vivek.hello.common.IHello")
public class Hello implements IHello
{
@Override
@WebMethod
public String sayHello(String userName)
{
return "Greeting " + userName + "! Have a nice day...";
}
}

Here we implement a webservice interface. EndpointInterface tells that which webservice interface we are implementing. Client will use this interface to acesss the webservice mthods.

Client Code using Dynamic Proxy:

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

import com.vivek.hello.common.IHello;
public class Test {
public static void main(String[] argv)throws Exception
{
try
{
ServiceFactory factory = ServiceFactory.newInstance();
//Lookup Service. . Service is entry point for a web service.
Service service = factory.createService(new URL(
"http://localhost:8080/JbossWS/?wsdl"), new QName(
"http://hello.vivek.com/", "HelloService"));
//A webservice can have di,fferent port. Port is like a library of methods.
//Set the location of wsdl.
IHello helloInterface = (IHello)service.getPort(new QName("http://hello.vivek.com/","HelloPort"),IHello.class);
//Invoke and Print the result.
System.out.println(helloInterface.sayHello("Vivek"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Output:-

Greeting Vivek! Have a nice day...

Static Stub:-
This is the simplest one. Here we get the wsdl file use a tool to compile(like WSDL2Java) the wsdl file which generats the client classes. We use these classes to call webservice client.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s