Saturday, July 30, 2011

Creating a webservice in 5 minutes

One day the boss ask you to create a webservice for a method and have it deployed immeadiately. What do you do? Don't panic. The quick way to deploy a webserviceis just a few annotations away. Firstly download the latest version of jboss preferably version 6 from the Jboss website. 


Then you annotate the bean you need to expose as webservice.
Create an interface for the bean first. Then annotate with @Remote and @WebService on the class. 
eg.


import javax.ejb.Remote;
import javax.jws.WebService;
@Remote
@WebService
public interface TestWebService {
    public void getTestCodes(List<String> qualification, String local) throws Exception;
}



Put @Stateless and @WebService on the implementation class.
eg.


import javax.ejb.Stateless;
import javax.jws.WebService;
@Stateless
@WebService
public class TestWebServiceImpl implements TestWebService{
    public void getTestCodes(List<String> qualification, String local) throws Exception{
        try {
            //perform method implementation here
        } catch (Exception e) {
        }
    }
}


After compiling and build the codes, package it into a jar file. You can do it by clicking File menu -> export on eclipse.
Copy the jar file into jboss deploy folder at server/default/deploy.
Start jboss.
You can see your webservice is running by going to your localhost url  http://localhost:8080/jbossws/services
If you want to expose it to be accessible by other pc within same network for testing remember to run jboss with command run.bat -b <your ip address>. Can only access the webservice locally only using the default jboss run.bat. 
Testing the webservice, use soapui client available for download at http://www.soapui.org/



No comments:

Post a Comment