Wednesday, September 29, 2010

Axis2 Java Web Services with NetBeans deployed on Tomcat

As we discussed earlier (previous post) you should set axis2 in to your tomcat web server.

Apply axis2 plugin

First you need to install "Axis2 Support" plugin to the net beans. Open the netbeans IDE and navigate to Tools -> Plugins. The plugins list will be displayed. If "Axis2 Support" is not showing there, please update the list.Then click the install button below and continue with installing...

Add apache web server to IDE

Ok, now our next task is to add apache to the netbeans IDE. To do that, go to the Services tab (or Window -> Services). Right click on servers and click "Add Server". It will popup following window. Select Tomcat 6.0 (according to your sever version) and continue.
You should navigate the folder which you have been placed Tomcat server. Add username and password (in my case I just used admin/admin). Then you will appear new server like this, You can simply start server by right click on tomcat and selecting start option.

Setup .aar file creating directory

Go to Tools -> Options. And now you will be able to see an separate tab for Axis2 with its logo. Click on that.
Now browse to your axis2.war file for "Target Location for axis2 .aar files:" field. And tick on use tomcat manager for deployment, enter username and password which you set earlier (in my case admin/admin). Then click ok.

Creating Web Services

Open a new project (File -> New Project). Select Java and Java Application. Add a desired project name (I used "CardValidator") and complete creating project. Then right click on newly created project and select New -> Axis2 Service From Java...
Then you will get popup window. There is nothing to change, in this case lets take this as Create Empty Web Service and click next. Then following step will visible. Give any name you wish as class name (in my case "MyClassValidator") and select package from list (in my case "cardvalidator"). And Finish it. Now its done. You will find a sample service call hello:String.
Important:You should right click on Axis2 Web Services and Deploy to Server, before test the sample program.
After that you can right click on hello:String and Test operation in browser. It will give following browser opening for your request.
Writing new methods is not a big deal.
Let's write a method to accept a card number and check whether it is valid one or not. If its starts with "1111" then its a valid card and should return true. Else should return false.
Simply write following java method on MyCardValidator class (which contains method hello as well)
public boolean validateCard(String cardNumber){

if(cardNumber.startsWith("1111")){
return true;
}else{
return false;
}
}

And now right click on MyCardValidator and deploy to server. Then your new service method will appear as a service. You can right click on it and test it.
Then your browser will open with link like this "http://localhost:8080/axis2/services/MyCardValidator/validateCard?cardNumber=XYZ". By changing XYZ you can check whether your service works properly.
Note: If your default browser is IE or Firefox, then the output will looks like a XML message. If you used chrome, the output will just be text values.

By placing the axis2.war file in any tomcat server after you creating it you can access to the web services in above manner.
Hope you got it correctly. If you got any problems pleas feel free to contact me by mailing or commenting here.
Your comments will be very useful for further writings.... :)
Thank you for visiting. :)

16 comments:

  1. Nice work!!!! keep it up Jana!!

    ReplyDelete
  2. Thanks buddy....
    But don't know who u r....

    ReplyDelete
  3. Great work, I was looking for this. thanks !!
    keep blogging...

    ReplyDelete
  4. while editing the service adding some method and jar file.
    I have set the configure option while making new webservice.

    I got successfully build message when I do deploy on server.
    but while listing the available services...I got error.

    Error: org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog at [row,col {unknown-source}]: [1,0] at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:260) at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:133) at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:502) at org.apache.axis2.util.XMLUtils.toOM(XMLUtils.java:602) at org.apache.axis2.util.XMLUtils.toOM(XMLUtils.java:581) at org.apache.axis2.deployment.repository.util.ArchiveReader.processWSDLs(ArchiveReader.java:337) at org.apache.axis2.deployment.ServiceDeployer.deploy(ServiceDeployer.java:67) at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136) at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:659)

    ReplyDelete
  5. Did you try with right clicking on server and choosing "Test service on browser" option???
    What happened then????

    ReplyDelete
  6. *correction not server, it should be service

    ReplyDelete
  7. ane bang therena deyak dapanko.. :) keep ups da guds works..

    ReplyDelete
  8. Hi,

    I have configure like that and run but it gives me error when it's deploying,

    E:\WorkDesk\Java Projects\MyWebservice\MyWebservice\nbproject\build-impl.xml:166: src.dir
    BUILD FAILED (total time: 0 seconds)

    please let me know how to fix it???????? thanks lot.

    ReplyDelete
  9. Is it only possible to create axis2 service in java application. I tried with dynamic web service and i was not able to find axis 2 service option in new webservice tab. But my axis is running on web browser. when we link to that file

    ReplyDelete
  10. please can you upload the how to handle a object which pass from the web service to client side in netbeans also? thanks lot. Great tutorial! :D

    send me an email if you can:
    dazimax@hotmail.com

    regards,
    Dasitha.

    ReplyDelete
  11. awesome tutorial helped me to figure out the step I was missing (^ ^,)

    ReplyDelete
  12. hi.. nice tutorial.. there is no problem in creating a web service in axis 2. also it works fine when testing the service using SOAP UI.
    But when i need to invoke the service using WSDL as client(i.e from JSP page), it invokes and runs the service once or twice. After that, the client page doesn't even invoke that service. I'm working in netbeans 6.5.1. May i know how to resolve this please?

    ReplyDelete
  13. See http://sbvb.com.br/jtanpws/ for another interesting tutorial

    By the way, I am looking for the source code of this plugin. If someone knows how to get it, please send me an email: sbvillasboas@gmail.com

    ReplyDelete