Friday, February 4, 2011

Create Basic SOAP Web Service using eclipse


A Simple SOAP Web Service Example in Eclipse 
Create Basic Web Service
  1. Create new Dynamic Web Project.
  2. Create new Java class (call it “DoesMagic”).
  3. Add method like this (perhaps):
package shl.com.util;

public class Maths {
                public String shailSays(String about){
                                return "There is some thing like: "+ about;
                }
}

4.      Right click on class you just made, New -> Other -> Web Services -> Web Service, you should get a form like the one below:


This will create all the necessary files to be able to publish your class as a web service.  If you want you can also create a client automatically by moving the slider – but what it generates may be hard to understand at first glance, so I have written a simple example of client code (see later on…).
5.  In order to run the web service – just,  Run as -> Run on Server.  Once the web service is running and alive on Tomcat – then we should be able to use it via some client code (see next bit).
Create Basic Web Service Client
Must create a project that has all the axis.jar type stuff in the WebContent folder – this can usually be made by generating a client application via the above wizard – you don’t have to use all the auto generated classes to access your web service, just do the following:
1.  Create a new Java Class – call it TestClient.
2.  Make it a main class, and enter the following code:
import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestClient {
      public static void main(String [] args)
      {
          try{
             String endpoint = "http://localhost:8080/WebServiceProject/services/Maths";
             Service service = new Service();
             Call call = (Call) service.createCall();
             call.setTargetEndpointAddress( new java.net.URL(endpoint) );
             call.setOperationName( new QName("http://util.com.shl", "shailSays") );

             String ret = (String) call.invoke( new Object[] {"AVACorp"} );
             System.out.println(ret);
             }
             catch(Exception e){
                 System.err.println(e.toString());
             }
      }

}


3.  Go to your web service project you made before and look at the source code for the wsdl file that has been created.  It’s in the folder WebContent -> wsdl.  Inside there you will find a wsdl that is the same name as your class.
-Shailendra kr. shail@AVACorp

10 comments:

  1. Nice Example Shail...!!!
    Expecting More Tutorials...!!!!

    ReplyDelete
  2. Very good example....To start with SOAP Webservice, This is a nice example. I searched lot of tutorial in google, this is the best one. Keep update more.

    Regards,
    Abhimanyu singh.

    ReplyDelete
  3. amazing nice one it helped me lot...

    ReplyDelete
  4. Feb 15, 2013 11:00:14 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
    WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
    (404)Not Found

    Getting this Error while runnig this

    ReplyDelete
  5. looks like some jar is missing..
    you can find these jars on www.findjar.com

    ReplyDelete
  6. looks like some jar is missing..
    you can find these jars on www.findjar.com

    ReplyDelete
  7. it is realy good one . help me alot ..

    ReplyDelete
  8. Hi Shailendra, is axis one way to create SOAP? here axis implementation is used to build the webservice. like axis do we have any other implementations to build web service.

    ReplyDelete
  9. Thanks.. Simple and Nice

    Add the below Jar's into the classpath, it will work.
    1. j2ee-1.4
    2. commons-logging-1.1.3
    3. commons-discovery-0.2
    4. axis-1.4

    ReplyDelete