A Simple SOAP Web Service Example in Eclipse (Ganymede)

Huh?  Not C#!!! – What’s all this “Rant in C Sharp” business then!?  Well, sometimes you have to be flexible in your job – well, lets just say we should be ‘language ambivalent’.

This is a VERY simple attempt to show show how to create a web service in Eclipse using Axis.  There is also some client code to show how you would interact with that web service.  For some reason I had to delve into a bazillion websites and scour a million books before I could make some sort of sense of it all – so just so you don’t have the same trouble – I have decided to publish it (well actually, it’s so that I don’t forget either).

Little disclaimer – I’m not saying that this is difinitive, I may be missing something, but it seems to work for me just now, and I haven’t gone into all the ‘bells and whistles’ of web services.  At the moment I’m writing down everything I learn and hopefully help others by sharing the knowledge.

(Using Java 1.5)

Create Basic Web Service

  1. Create new Dynamic Web Project.
  2. Create new Java class (call it “DoesMagic”).
  3. Add method like this (perhaps):
public class DoesMagic{
    public String doBasicStuff(String message)
    {
        return "This is your message: " + message;
    }
}

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/TestWebService/services/DoesMagic";

       Service service = new Service();
       Call call = (Call) service.createCall();
       call.setTargetEndpointAddress( new java.net.URL(endpoint) );
       call.setOperationName( new QName("http://testPackage.fc.com", "doBasicStuff") );

       String ret = (String) call.invoke( new Object[] {"Hello World"} );
       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.

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.

Conclusion

This is not indepth – this may not even be 100% correct, but it just gives you a taste of what can be achieved – it’s up to you now to read up more on Java Web Services.  Good luck !

PS – Take a sec just to vote whether this article helped you or not – see widget below.  If not, tell me what’t wrong with it and I’ll try and improve it.  I love feedback so I look forward to your comments!

Advertisements
A Simple SOAP Web Service Example in Eclipse (Ganymede)

30 thoughts on “A Simple SOAP Web Service Example in Eclipse (Ganymede)

  1. Mescg Roupe says:

    Nice, short, and to the point. Also like how you imported only the classes you needed in your client code example.
    Might want to add a note that you need to install a container on your machine AND create a new Server via the Eclipse Window, Preferences, Server, Runtime Environments and point to that installation.

  2. Hi Simon,
    thanks for the post, i have faced a problem at the end of step 4, when i just select the method to be exposed and i click on next i get the error IWAB0398E Error in generating WSDL from Java: java.lang.ClassNotFoundException: edu.uji.ws.OGCWSAdapter. OGCWSAdapter is my DoesMagic class. any ideas?

  3. suji says:

    hi
    can u explain me
    call.setOperationName( new QName(“http://testPackage.fc.com”, “doBasicStuff”) );

    what is “http://testPackage.fc.com”

  4. kris says:

    hi suji,

    it is the targetnamespace value that was generated from the package name you used when creating the DoesMagic POJO. You can find the exact value in your web service WSDL.

    hth,

    Kris

  5. Carmen says:

    In line
    Call call = (Call) service.createCall();
    I get the error “The type javax.xml.rpc.ServiceException cannot be resolved. It is indirectly referenced from required .class files”.
    I’ve downloaded the axis.jar and I’m using Eclipse Ganymede. Do I need another .jar? Or is my error somewhere else?
    Sorry for my English! I hope someone can help me find the right way!

  6. divyesh sharma says:

    when i try to run the application it gives this exception java.net.ConnectException: Connection refused: connect please give me the solution.

  7. Simon says:

    Sorry – it’s been a while now and I never looked at authentication in any case. Sorry I can’t be much help. If I have time to find out I will post and let you know though.

  8. John says:

    I know this is an old thread, but I just came across it. I tried running through the steps, when I try to run the client, I get this:

    org.xml.sax.SAXException: Processing instructions are not allowed within SOAP messages

  9. Michal says:

    I like the tutorial, but I don’t like the exception handling
    catch(Exception e){

    21 System.err.println(e.toString());

    22 }
    .Throw the exceptions out or use e.printStackTrace(), from the System.err.println(e.toString()); you can see only the exception message.

  10. Kumar says:

    Oct 12, 2012 5:16:44 PM org.apache.axis.utils.JavaUtils isAttachmentSupported
    WARNING: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
    java.net.ConnectException: Connection refused: connect

    I am getting the above error. PLease help me.
    Kumar SHorav

  11. Thanks for one’s marvelous posting! I actually enjoyed reading it, you happen to be a great author.I will make certain to bookmark your blog and will come back sometime soon. I want to encourage yourself to continue your great writing, have a nice evening!

  12. I am extremely impressed with your writing skills and also with the
    layout on your weblog. Is this a paid theme or did
    you customize it yourself? Anyway keep up the nice quality
    writing, it is rare to see a great blog like this one today.

  13. Hey there this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting a blog soon but have no
    coding experience so I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  14. I do believe all of the ideas you have offered to
    your post. They’re really convincing and will certainly work.

    Nonetheless, the posts are very brief for novices.
    May you please extend them a little from next time? Thanks
    for the post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s