A Simple SOAP Web Service Example in Eclipse
Create Basic Web Service
- Create new Dynamic Web Project.
- Create new Java class (call it “DoesMagic”).
- 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.
Or go to browser and give : http://localhost:8080/WebServiceProject/services/Maths?wsdl
-Shailendra kr. shail@AVACorp
Nice Example Shail...!!!
ReplyDeleteExpecting More Tutorials...!!!!
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.
ReplyDeleteRegards,
Abhimanyu singh.
amazing nice one it helped me lot...
ReplyDeleteFeb 15, 2013 11:00:14 AM org.apache.axis.utils.JavaUtils isAttachmentSupported
ReplyDeleteWARNING: 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
looks like some jar is missing..
ReplyDeleteyou can find these jars on www.findjar.com
looks like some jar is missing..
ReplyDeleteyou can find these jars on www.findjar.com
it is realy good one . help me alot ..
ReplyDeleteHi 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.
ReplyDeletegood one...
ReplyDeleteThanks.. Simple and Nice
ReplyDeleteAdd 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