Saturday, December 8, 2012

Create java web service in Eclipse using Axis2 (Part 03)

This is the third part of create java web service using Axis2 tutorial. In this post I'm going to create a simple client program for the web service developed in previous tutorial. Before creating this program you have to complete my previous tutorials.

1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)

Before implementing the client first we have to find the wsdl file.
First run the created project on Tomcat server. To do this right click on project select Run As then select Run on Server.


Click on Services

Select Hello which is your web service class name. Under Available Operations you can see your web service operation.

 After clicking on Hello you can see the  WSDL file. Copy WSDL link to the clip board.



OK. We coppied the WSDL link.

Now we have to create the web service client.
Right click the web service class select New >> Other >> Web Services >> Web service Client. 



Click Next. Paste link of the wsdl file to the Service definition box. (If you want to access the wsdl in another computer via a net work replace "localhost" in the WSDL url with the ip address of the server machine)
Make sure that the Server runtime is Tomcat & the Web service runtime is Axis2 by clicking the links underlined red color in the following diagram.

Client type : Java Proxy




 


Click finish to generate Callback Handler & the Stub in same package where the web service class exists.(If you want to change the client package click next & give a package name to Custom package name as you wish)

Then create new class. Class name that I used is Client. Here is the project structure.



Following code shows how we can invoke the web service method using auto generated Stub. Paste this code to newly created class. This client class also in the same package with  web service class. 

 
package org.webapp.ws;
import org.webapp.ws.HelloStub.SayHello;

public class Client {
 public static void main(String[] args) throws Exception {
     HelloStub stub = new HelloStub();
    
     //Creating the request
     org.webapp.ws.HelloStub.SayHello  request = new  org.webapp.ws.HelloStub.SayHello();
     request.setName("Java web");
   
     //Invoking the service
     org.webapp.ws.HelloStub.SayHelloResponse response  = stub.sayHello(request);
     System.out.println("Response : " + response.get_return());
    }
}


Right click Client class and run the client as a java application.



Result :


Click here to download the complete project with client.

If you find this post helpful don't forget to leave a comment. Your comments always encourage me to write more!

Happy coding!

30 comments:

  1. Thanks sooooooooooo soooooooo soooooo much

    ReplyDelete
  2. Thanks soooo much, I can able create and run the web service... It is very use full.

    ReplyDelete
  3. The HelloStub.java is showing errors, how these can be fixed? "Multiple markers at this line
    - HashMap is a raw type. References to generic type HashMap should be
    parameterized
    "

    ReplyDelete
  4. Hello!! the tutorial is really very nice. But I am getting the following error in HelloStub.java class, how it could be resolved?

    Multiple markers at this line
    - HashMap is a raw type. References to generic type HashMap should be
    parameterized

    ReplyDelete
  5. Thanks buddy ..really useful..........

    ReplyDelete
  6. Hello. I've a problem on the line "request.setName("Java web");". setName is undefined for the type HelloStub.SayHello.

    Any solution?
    Thanks :-)

    ReplyDelete
  7. Great tutorial, but I get the following error in Client class on :request.setName("Java web"); setName is not defined ;

    Any solution???
    Thanks.

    ReplyDelete
  8. This is a very good tutorial!

    ReplyDelete
  9. Excuse me.. I followed the whole process (all 3 parts..)
    and when I´m running the client class as a java aplication I have this error:

    Exception in thread "main" org.apache.axis2.AxisFault: unknown
    at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
    at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
    at org.apache.ws.BaseDatosWSStub.digaHola(BaseDatosWSStub.java:197)
    at org.apache.ws.Client.main(Client.java:18)

    ReplyDelete
  10. I followed the whole tuotrial.. the 3 parts and when I´m running the client class as a java application I come across this message:

    Exception in thread "main" org.apache.axis2.AxisFault: unknown
    at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
    at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
    at org.apache.ws.BaseDatosWSStub.digaHola(BaseDatosWSStub.java:197)
    at org.apache.ws.Client.main(Client.java:18)

    ReplyDelete
  11. i tried this code... when i run andriod file the emulator shows force close...i changed all the things..but not working

    ReplyDelete
  12. Thank You Soooooooo Much ... By the way is this Soap/rest web service ?

    ReplyDelete
  13. Thank You So Much. By the way is this rest or soap web service?

    ReplyDelete
  14. Excellent work, I followed the two example and both of them worked properyly with zero problem.

    ReplyDelete
  15. Thank You So Much...

    using this application we can run with in eclipse only we can reflect actual Tomcat server

    My Question is how can deploy our app in Tomcat_Home/webapps/axis2/our app of our local mession

    Thank in advance....

    ReplyDelete
  16. Thank You So Much

    I Have a Small Question ..
    using this we can deployee our app in eclipse only but not in local Tomcat server

    i mean not in Tomcat_Home/webapps/axis2/WebApp

    How to reflect in tomcat local metion

    thanks in advance...

    your's Pavan R

    ReplyDelete
  17. can anybody help me ?i m not getting name of method in service list i.e. deployed web service i only got web service class name but when i get the page in browser it shows me no operation specified what is wrong there.

    ReplyDelete
  18. i got this error:
    http status 404-
    is there any modification required for web-INF of build.xml or other file required?

    ReplyDelete
  19. i got status 404 error after all above procedure.
    is ther any other modification required in another folder/file like any xml file etc?

    ReplyDelete
  20. Hey the best tutorial i have ever found . Please can u explain me how it is working.I didn't understood the flow of code. how it it connected to the webservice class. The three class has been generated what is the significant of those class.

    ReplyDelete
  21. Hey the best tutorial i have ever found . Please can u explain me how it is working.I didn't understood the flow of code. how it it connected to the webservice class. The three class has been generated what is the significant of those class.

    ReplyDelete
  22. Please tell me how to find soapAction from generated wsdl file.Please help me .

    ReplyDelete
  23. Please help to find soapAction form created wsdl file or any other option

    ReplyDelete
  24. Excellent compilation, I could do to great extend except I did not understand

    request.setName("Java web"); in client code

    suppose this is my class

    public class Conv {
    public double ftoC(double x)
    {
    return (5.0/9.0)*(x-32.0);
    }
    public double ctoF(double y)
    {
    return ((9.0/5.0)*y)+32.0;
    }
    }
    what will be the possible client code?

    ReplyDelete
  25. Thank You So Much! The Brazilian Students thanks!

    ReplyDelete
  26. Great Tutorials, tons of thanks!!

    ReplyDelete
  27. Nice Job!! How can I use this web service from an android client!!

    ReplyDelete
  28. Great article,gone through your page ,really it's an amazing and useful information.Thanks for sharing.
    Web Development in Bangalore|Website Designing Company Bangalore

    ReplyDelete