June 19, 2018
Hot Topics:

Very Simple Object Access Protocol with Java

  • January 15, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

Now, step by step we will find out what happens. In Step 1, HelloWorldClient will connect to a SOAP RPC Router, requesting our SOAP Service and passing a String value containing our name to it. The SOAP RPC Router will check whether it has deployed theSOAP Service or not. If it is found, it will pass data to the SOAP Service and call the specified method. This is Step 2. The Method of the SOAP Service will be executed, and will return some String value (this will be its answer to SOAP Client) (Step 3). The SOAP RPC Router will just redirect this data to the SOAP Client in Step 4. That is all. All data transfer in Step 1 and Step 4 will be done in SOAP Envelopes (hold on for a while, and you will see what they look like). As you can see, the algorithm is pretty simple, too, so we are ready to take care about the code.

First of all, we will create the SOAP Service. The code is below, and do not forget to put it into a HelloWorld/ directory (and, eventually, include this directory into your CLASSPATH):

1: // SOAPService.java
2: package HelloWorld;
3: public class SOAPService {
4:   public String sayHi(String x) {
5:     return("Hello my friend, " + x + "! Glad to see you!");
6:   }
7: }

(Please note that the numbers on the left to the text are just for your information. You do not need to type them!)

I guess it's too easy to add any comments. So, just compile it with the following command:

javac SOAPService.java

Secondly, once we have the SOAP Service ready, we need to deploy it with the SOAP Service Manager. There are a few ways to do that, but I suggest the easiest, to my mind, for the beginner in SOAP understanding. We assume that your Web server (Tomcat or any other) is already running, and you installed SOAP properly, so when browsing to http://localhost:8080/soap/ you will see the Apache SOAP Welcome page. Click Run the admin client, then Deploy. You will get a screen, on which you need to fill in the next fields—ID, Scope, Methods, Provider Type, and Java Provider. You can forget about all the other fields until you need them. We do not need them for our "Hello World" example now. So, the values will be the following:

Provider Type:java
Java Provider - Provider Class:     HelloWorld.SOAPService
Java Provider - Static?No

A few comments: ID is field in which we will identify our SOAP Service from SOAP Client. The Methods field contains a list of methods that the SOAP Service provides. Java Provider - Provider Class is the name of the SOAP Service Java class.

Now, click the Deploy button and your service will be deployed. Again, please pay attention to correctly setting the CLASSPATH environment variable, so your HelloWorld.SOAPService class can be found and all necessary jars can be found too. This is the common mistake almost everybody makes. And now, you can click List and you will see your service deployed. Congratulations!

And, last but not least, let's create the SOAP Client. The code will look a bit more complex, but it's not in reality—just a bit long.

 1: // HelloWorldClient.java
 2: import java.io.*;
 3: import java.net.*;
 4: import java.util.*;
 5: import org.apache.soap.*;
 6: import org.apache.soap.rpc.*;
 7: public class HelloWorldClient {
 8:   public static void main(String[] arg) throws Exception {
 9:     Call c = null;
10:     URL url = null;
11:     Vector params = null;
12:     Response rep = null;
13:     String ourName = "Superman";
14:     String ourUrn = "urn:HelloWorld_SOAPService";
15:     String ourMethod = "sayHi";
16:     url = new URL("http://localhost:8080/soap/servlet/
17:     System.out.println("Passing to our deployed "+ourUrn+"
                            our name ("+ourName+"): ");
18:     c = new Call();
19:     c.setTargetObjectURI(ourUrn);
20:     c.setMethodName(ourMethod);
21:     c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
22:     params = new Vector();
23:     params.addElement(new Parameter("ourName", String.class,
                                         ourName, null));
24:     c.setParams(params);
25:     System.out.print("and its answer is: ");
26:     rep = c.invoke(url, "");
27:     if (rep.generatedFault()) {
28:       Fault fault = rep.getFault();
29:       System.out.println("\nCall failed!");
30:       System.out.println("Code = " + fault.getFaultCode());
31:       System.out.println("String = " + fault.getFaultString());
32:     } else {
33:       Parameter result = rep.getReturnValue();
34:       System.out.print(result.getValue());
35:       System.out.println();
36:     }
37:   }

(Please note that the numbers on the left to the text are just for your information. You do not need to type them!)

I would like to add a few comments. In Line 13, we set our name, which we will pass to the SOAP Service. In Line 14, we set the ID of the service that we will invoke, and in Line 15, the method of this service. With this ID, this service should be deployed in the SOAP Service Manager. We do not set any other values, and just work with these. You can get information about the functions from the official documentation on SOAP that comes with your SOAP package. Explaining them goes beyond the scope of this article.

Compile the SOAP Client in the following way:

javac HelloWorldClient.java

To crown it all, let's check whether everything is ready for our test. Tomcat is running, all environment variables are set correctly, the SOAP Service is compiled and deployed, and the SOAP Client compiled successfully. Okay, let's run it and you will see this screen:

Click here for a larger image.

As you can see, our SOAP Client successfully sent its name and received an answer with the SOAP protocol. And, as promised above, here is the source of the SOAP envelopes that were sent to and received from the SOAP Service.

SOAP Envelope that was sent to SOAP Service

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/
<ns1:sayHi xmlns:ns1="urn:HelloWorld_SOAPService"
<ourName xsi:type="xsd:string">Superman</ourName>

SOAP Envelope that was received from SOAP Service

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/
<ns1:sayHiResponse xmlns:ns1="urn:HelloWorld_SOAPService"
<return xsi:type="xsd:string">Hello my friend, Superman!
                              Glad to see you!</return>

To understand what all these tags in the SOAP Envelopes are, I suggest that you spend some time reading the http://www.w3.org/2001/06/soap-envelope namespaces specification.

I hope this article was at least a little help for you in understanding SOAP technology. This technology is simple and interesting, powerful, and flexible; it is used in many Web applications and the number of them is growing. It's worth learning SOAP, at least just to know what is it and how it works.

© Olexiy Prokhorenko
Co-author: Alexander Prohorenko

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date