Sunday, 16 September 2012


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):

1public class DoesMagic{
2    public String doBasicStuff(String message)
3    {
4        return "This is your message: " + message;
5    }
6}

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:

01import javax.xml.namespace.QName;
02import org.apache.axis.client.Call;
03import org.apache.axis.client.Service;
04 
05public class TestClient {
06 
07public static void main(String [] args)
08{
09    try{
10       String endpoint = "http://localhost:8080/TestWebService/services/DoesMagic";
11 
12       Service service = new Service();
13       Call call = (Call) service.createCall();
14       call.setTargetEndpointAddress( new java.net.URL(endpoint) );
15       call.setOperationName( new QName("http://testPackage.fc.com", "doBasicStuff") );
16 
17       String ret = (String) call.invoke( new Object[] {"Hello World"} );
18       System.out.println(ret);
19       }
20       catch(Exception e){
21           System.err.println(e.toString());
22       }
23}
24}

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.
4. You need to look at the wsdl and find the endpoint – which looks similar to:
"http://localhost:8080/TestWebService/services/DoesMagic"
and you need to get the namespace and the method name you want to invoke  (as shown above, should be in a very similar format).
5. In this case, when we get to the point that the call.invoke command is being issued it is casting the result to a String and we are sending in a String called “Hello World” – this should create a message in the console like “This is your message: Hello World”.
6. To test the class, just run the project as a Java Application and you should see the result in the Console printed out.

No comments:

Post a Comment