Very Simple Object Access Protocol with Java
By Olexiy & Alexander prokhorenko
Now let's take a step-by-step look at what happened. In step 1, helloworldclient connects to a soap RPC router, requests our soap service, and passes a string containing our name to it. The soap RPC router checks whether the soap service has been deployed. If it is found to be deployed, it will pass data to the soap service and call a specific method. This is step 2. Then the soap service method is executed and a string value (this value is the response or response of the soap client) is returned (Step 3 ). In step 4, the soap RPC router only redirects the data to the soap client. All data transmitted in step 1 and Step 4 is completed through SOAP envelope. As you can see, the algorithm is quite simple, so we only want to focus on the actual code.
First, we need to create a soap service. The following is its code. Do not forget to put it into the helloworld/directory (it must be included in your classpath ):
1: // SOAPService.java2: package HelloWorld;3: public class SOAPService {4: public String sayHi(String x) {5: return("Hello my friend, " + x + "! Glad to see you!");6: }7: }
It is easy to add any comments. To compile it, you only need to use the following command:
javac SOAPService.java
Step 2: Once we have prepared the soap service, we need to deploy it using the soap Service Manager. This can be implemented in many ways, but in order to make it easier for beginners to understand soap, I provide the easiest way. Assume that your web server (tomcat or other) is running properly and you have installed soap correctly. When the browser accesses http: // localhost: 8080/soap/, you will see the welcome page of Apache SOAP. ClickRun the Admin Client, And thenDeploy. You will get a screen display where you need to fill in the ID, scope, method, provider type and Java provider Information to the form field. You can ignore all other form fields unless you really need their information. We do not need them in our "helloworld" example. Therefore, we fill in the following values:
ID: |
URN: helloworld_soapservice |
Scope: |
Application |
Methods: |
Sayhi |
Provider type: |
Java |
Java provider-provider class: |
Helloworld. soapservice |
Java provider-static? |
No |
Note: ID is the unique name of the soap service from the soap client. The method contains a series of methods provided by the soap service. Java provider-provider class is the name of the soap service Java class.
Now, clickDeployButton, your service will be deployed. Again, pay attention to setting the classpath environment variables correctly. Then, your helloworld. soapservice class can be found, and all necessary jar packages can also be found. This is a common mistake that almost everyone will make. Now you can clickList, You will see that your service has been deployed. Congratulations!
Finally, let's create a soap client. The code looks a little complicated, but in reality it won't be so 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/ rpcrouter");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: }38:}
Here are some explanations. In row 3, we set our name, which will be passed to the soap service. In row 14th, we set the service ID we want to call, and the service method set in row 15th ). With this ID, the service can be deployed in the soap Service Manager. We didn't set any other values. We only needed the basic values just now to work properly. You can get the relevant information from the official soap documentation, which comes from the soap package and their explanations are beyond the scope of this article.
Compile the soap client as follows:
javac HelloWorldClient.java
To complete it successfully, let's check whether everything is ready for our tests. Tomcat is running. All environment variables are correct. The soap service is compiled and deployed, and the soap client is compiled successfully. OK, let's run it and you will see this screen:
As you can see, our soap client successfully sends its name and receives a response using the SOAP protocol. As mentioned above, the soap Service sends and receives SOAP envelope. This is the source code of SOAP envelope.
QuiltSendSOAP envelope to the soap Service
<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/ soap/envelope/" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><ns1:sayHi xmlns:ns1="urn:HelloWorld_SOAPService" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/ soap/encoding/"><ourName xsi:type="xsd:string">Superman</ourName></ns1:sayHi></SOAP-ENV:Body></SOAP-ENV:Envelope>:
From soap ServiceReceiveSOAP envelope
<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/ soap/envelope/" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><ns1:sayHiResponse xmlns:ns1="urn:HelloWorld_SOAPService" SOAP-ENV:encodingStyle="http://schemas.xmlsoap. org/soap/encoding/"><return xsi:type="xsd:string">Hello my friend, Superman! Glad to see you!</return></ns1:sayHiResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
To understand the meaning of all labels in SOAP envelope, I suggest you take a moment to readHttp://www.w3.org/2001/06/soap-envelopeNamespace specifications.
I hope this article will help you understand the soap technology. This technology is simple, interesting, powerful, and elastic. It is used in many web applications, and the number of such applications is also increasing. Learning soap is worthwhile. At least you need to know what it is and how it operates.
Translated by caiyi0903 (willpower), 2004.1.17