SOAP Purification Wired Protocol (ii): Introduction to Apache Soap (2)

Source: Internet
Author: User
Tags exit command line contains soap parent directory serialization xmlns tomcat
Apache three, with JavaBean HelloWorld example
As mentioned earlier, Apache soap provides a number of predefined serialization and drag methods, including the serialization and drag of Java vectors, enumeration, arrays, JavaBean as parameters and return values. In this section, I will modify the HelloWorld service, passing through a javabean to the username that receives the Hello information.

3.1, HelloWorld service
The complete code for the rewritten HelloWorld service is as follows:


Package Hello;
public class HelloServer
{
public string Sayhelloto (string name)
{
System.out.println ("Sayhelloto (String name)");
Return "Hello" + name + ", how are you doing?"
}
Public String Sayhelloto (Name thename)
{
System.out.println ("Sayhelloto (Name thename)");
Return "Hello" + thename.getname () + ", how to Are you doing?"
}
}
The code for the service is still simple, and is still similar to the HelloWorld service when not JavaBean. However, this means that the most complex tasks are transferred to the client. In fact, the only difference between this version of the service and the previous version is that there is now an overloaded Sayhelloto () method. The overloaded method in the above code is shown in bold text.

Overloaded methods require a reference to the name JavaBean. The Name JavaBean is defined as follows:


Package Hello;
public class Name
{
private String name;
Public String GetName ()
{
return name;
}
public void SetName (String name)
{
THIS.name = name;
}
}
3.2. Deployment Services
When you deploy a service that uses JavaBean, you need to provide some additional information for the Apache SOAP server. As a result, the process of deploying services is a little more complex now.

Deploying services with Administrative Tools

To deploy this new version of the HelloWorld service using administrative Tools, first follow the steps described earlier, but do not click the Deploy button this time. Now, enter 1 in the number of mappings input box, which means that we will give you a map (that is, name JavaBean) information. Immediately below mappings there is a table, we need to use the first line of the table. Preserves the value of the encoding style to soap and sets the namespace URI to the ID of the object: In this case, it is Urn:hello. Next, set the local and Java type input boxes to the full name of name JavaBean, that is, hello. Name. Finally, Java to XML serializer and XML to Java The Deserializer input box is set to Org.apache.soap.encoding.soapenc.BeanSerializer, which is a class that implements the serializer and Deserializer interfaces, and is used to serialize and drag the row name JavaBean. If you use more JavaBean (for example, an address bean), you should enter information for other beans in this table, and you should also update the value of the number of mappings input box to reflect how many rows are actually being used in the table.

Deploying services from the command line

To deploy from the command line, we simply modify the XML deployment descriptor file passed in as a command-line argument. The modified XML file looks like this:


<isd:service xmlns:isd= "http://xml.apache.org/xml-soap/deployment" id= "Urn:hello" >
<isd:provider type= "java" scope= "Application" methods= "Sayhelloto" >
<isd:java class= "Hello. HelloServer "static= false"/>
</isd:provider>
<isd:mappings>
<isd:map encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"
xmlns:x= "Urn:hello" Qname= "X:hello. Name "
Javatype= "Hello. Name "
Java2xmlclassname= "Org.apache.soap.encoding.soapenc.BeanSerializer"
Xml2javaclassname= "Org.apache.soap.encoding.soapenc.BeanSerializer"/>
</isd:mappings>
</isd:service>
As in the previous example, the XML code contains the same information as the information provided through the Web interface's administrative tools.

3.3. HelloWorld client program
As with the first example, the client program is more complex and more interesting. Instead of looking at the entire client, I'm going to introduce the differences between the two client versions of the program. Because a parameter to the calling method (which is the only parameter in this case) is a javabean, you must manually set a type mapping registry entry. This task is accomplished by first creating an instance of the Org.apache.soap.encoding.SOAPMappingRegistry class and then calling its Maptypes () method. As the Maptypes () method name predicts, it is used to register a previously unknown type, such as a custom JavaBean. The parameters of the Maptypes () method include the encoding to use, the qualified JavaBean name, the full class name of the type, the serializer, and the drag row-builder. In this case, the serialization task is performed by a standard bean serializer. A qualified JavaBean name contains the name of an element, including the namespace to which it belongs. In this case, the qualified name of the name JavaBean is composed of a namespace URI (Urn:hello) and a local name (Hello.name). Take a look at the following code fragment:


To create a type mapping registrar
Soapmappingregistry SMR = new Soapmappingregistry ();
Beanserializer beanser = new Beanserializer ();
Mapping type
Smr.maptypes (Constants.ns_uri_soap_enc,
New QName ("Urn:hello", "Hello.") Name "), hello. Name.class, Beanser, Beanser);
Next, the client program must tell the call object to use the new registrar instead of the default registrar. To do this, we invoke the Setsoapmappingregistry () method of the Call object as follows:

Call.setsoapmappingregistry (SMR);
Once you have set the type mapping registrar manually, you must then set the parameters for the call object. This step can be done in the way described earlier, except that now we no longer use the name of the string type as an argument, but instead use JavaBean as the argument, as follows:


Set Call parameters
Vector params = new vector ();
Name thename = new name ();
Thename.setname (name);
Params.addelement (New Parameter ("name"), hello. Name.class, thename, null));
Call.setparams (params);
The remainder of the client program is the same as the original version. Listing 3 shows the full client code:


Listing 3:client2.java
Package Hello;
Import Java.net.URL;
Import Java.util.Vector;
Import org.apache.soap.SOAPException;
Import org.apache.soap.Constants;
Import Org.apache.soap.Fault;
Import Org.apache.soap.rpc.Call;
Import Org.apache.soap.rpc.Parameter;
Import Org.apache.soap.rpc.Response;
Import Org.apache.soap.encoding.SOAPMappingRegistry;
Import Org.apache.soap.encoding.soapenc.BeanSerializer;
Import Org.apache.soap.util.xml.QName;

public class Client2
{
public static void Main (string[] args) throws Exception
{
if (args.length = 0)
{
System.err.println ("Usage:java hello.") Client [Soap-router-url] ");
System.exit (1);
}
Try
{
URL url = null;
String name = NULL;
if (args.length = 2)
{
url = new URL (args[0]);
name = Args[1];
}
Else
{
url = new URL ("Http://localhost:8080/apache-soap/servlet/rpcrouter");
name = Args[0];
}
Constructing the Calling Object
Call call = new Call ();
Call.settargetobjecturi ("Urn:hello");
Call.setmethodname ("Sayhelloto");
Call.setencodingstyleuri (CONSTANTS.NS_URI_SOAP_ENC);
To create a type mapping registrar
Soapmappingregistry SMR = new Soapmappingregistry ();
Beanserializer beanser = new Beanserializer ();
Mapping type
Smr.maptypes (Constants.ns_uri_soap_enc,
New QName ("Urn:hello", "Hello.") Name "),
Hello. Name.class, Beanser, Beanser);
Call.setsoapmappingregistry (SMR);
Setting parameters
Vector params = new vector ();
Name thename = new name ();
Thename.setname (name);
Params.addelement (New Parameter ("name"), hello. Name.class,
Thename, null));
Call.setparams (params);
Emit call
Response resp = null;
Try
{
resp = call.invoke (URL, "");
}
catch (SoapException E)
{
System.err.println ("Caught SoapException" +
E.getfaultcode () + "):" + e.getmessage ());
System.exit (-1);
}

Check Answer
if (!resp.generatedfault ())
{
Parameter ret = Resp.getreturnvalue ();
Object value = Ret.getvalue ();
System.out.println (value);
}
Else
{
Fault Fault = Resp.getfault ();
SYSTEM.ERR.PRINTLN ("Generated fault:");
System.out.println ("Fault Code =" + Fault.getfaultcode ());
System.out.println ("Fault String =" + fault.getfaultstring ());
}
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
Iv. compiling and running the program
Now that the whole development of the program has been completed, it is time to run it. However, we first compile the service program and the client program.

Create a Hello directory to copy Client1.java, Client2.java, and Helloserver.java to this directory. I put the Hello directory under the example directory of Apache Soap (that is, E:\soap-2_0\samples). When compiling a program, the Classpath only contains the parent directory of the Hello directory (i.e. E:\soap-2_0\samples), Soap.jar, and Xerces.jar. I compile the program with the following batch command:


Set classpath=e:\soap-2_0\samples\; E:\soap-2_0\lib\soap.jar; E:\xerces-1_2_0\xerces.jar
Javac-d.. Helloserver.java Client.java Client2.java
Note: Execute the batch command file from the Hello directory.

To use this service, in addition to deploying it, you need to modify the classpath of the Web server to ensure that the Web service can find the Hello.helloserver class-for this example, this refers to the E:\soap-2_0\ Samples added to the Web server's classpath. After making the necessary modifications to the Classpath, restart the Web server. You can then run the client program. Below is my run hello. Client's Batch command file:

Set classpath=e:\soap-2_0\samples\; E:\soap-2_0\lib\soap.jar; E:\xerces-1_2_0\xerces.jar
Java Hello. Client Tarak
The classpath here is the same as the classpath used to compile the program.

Finally, run hello. The Client2 Batch command file can be as follows:

Set classpath=e:\soap-2_0\samples\; E:\soap-2_0\lib\soap.jar; E:\xerces-1_2_0\xerces.jar
Java Hello. Client2 Tarak
Watch the Web server's console window to see which methods of the HelloWorld service are being invoked when running two different client programs.

Concluding remarks
In this article, I introduced how to use the Apache SOAP implementation to create a simple soap based service. Another important competitor in the context of SOAP implementations is Microsoft. Unfortunately, "pure" Java developers have a hard time working with Microsoft, because its implementation includes COM objects.

In the next article, I'll introduce another way to create services with Apache SOAP support: Scripting languages like JavaScript, not java. In addition, I would like to introduce a very good JavaScript engine, that is, rhino.
Reference Resources
    • The SOAP 1.1 specification for the consortium:
      http://www.w3.org/TR/SOAP/
    • Download Apache SOAP:
      http://xml.apache.org/dist/soap/
    • For more information about the IBM SOAP Project:
      Http://www.alphaworks.ibm.com/tech/soap4j
    • A checklist of the available features of Apache soap:
      Http://xml.apache.org/soap/features.html
    • Apache License Agreement:
      Http://www.apache.org/LICENSE.txt
    • Download Tomcat 3.1
      http://jakarta.apache.org/builds/jakarta-tomcat/release/v3.1.1/bin/
    • Download Apache Xerces version 1.2:
      http://xml.apache.org/dist/xerces-j/
    • "MS SOAP SDK vs IBM Soap4j:comparison & Review," James Snell (O ' Reilly):
      Http://windows.oreilly.com/news/soapreview_0600.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.