Vi. Service Access
Get Service Access
Generally, soap messages are transmitted in post mode, but can also be accessed in get mode. For example, the following is a service named "helloworld". The source code is as follows:
File helloworld. JWS
Public class helloworld
{
Public String helloworld ()
{
System. Out. println ("Hello world! "); // Print the output on the server
Return "Hello world! "; // Returns the corresponding string
}
}
This service provides an operation named "helloworld" without an entry parameter, and returns a content of "Hello World! And print "Hello World!" on the server !", Put this file in "... In the/webapps/axis directory, you can directly access the service using the get method. The access address is http: // localhost: 8080/axis/helloworld. JWS? Method = helloworld. The returned SOAP envelope message is displayed, and the server displays the "Hello World!" message !" This indicates that the helloworld service has been successfully accessed, and the generated SOAP envelope message is:
<? XML version = "1.0" encoding = "UTF-8"?>
-<Soapenv: envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
-<Soapenv: Body>
-<Helloworldresponse soapenv: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/">
<Helloworldreturn xsi: TYPE = "XSD: string"> Hello world! </Helloworldreturn>
</Helloworldresponse>
</Soapenv: Body>
</Soapenv: envelope>
VII. Client Service Access Programming
Axis provides a set of APIs to implement soap. You can see the API documentation of axis from http: // localhost: 8080/axis/docs/apidocs/index.html.
Here, org. apache. axis. client. call and org. apache. axis. client. service is a common class. When a client program wants to access a web service, it generates a service object and a call object for the client. before accessing the service, first, you must set parameters for the call object, including the service location, operation name, entry parameter, and return value type. Finally, call the call object's invoke method to access the service.
The following is a routine for a client to access web services-axistest. Java:
File axistest. Java
Package axisexercise;
Import org. Apache. axis. Client. call;
Import org. Apache. axis. Client. Service;
Import org. Apache. axis. encoding. xmltype;
Import javax. xml. rpc. parametermode;
Public class axistest
{
Public static void main (string [] ARGs) throws exception
{
Create service and call objects, which are standard JAX-RPC objects used to store the data (metadata) of service calls ).
Service = new service ();
Call call = (CALL) service. createcall ();
//////// Access the distance service that is instantly released
// Set the Access Point
Call. settargetendpointaddress ("http: // localhost: 8080/axis/distance. JWS ");
// Set the operation name
Call. setoperationname ("convertmile2kilometre ");
// Set the entry parameters
Call. addparameter ("OP1", xmltype. xsd_double, parametermode. In );
// Set the Response Parameter type
Call. setreturntype (xmltype. xsd_double );
Double d1 = new double (190 );
// Call the service. In the invoke method, an array containing the call parameters is input.
System. Out. println (d1 + "Miles equivalent to" +
Call. Invoke (new object [] {D1}) + "km! ");
//////// Access the customized capacity service.
Call = (CALL) service. createcall ();
// Set the Access Point
Call. settargetendpointaddress ("http: // localhost: 8080/axis/services/capacity ");
// Set the operation name
Call. setoperationname ("convertgallon2litre ");
// Set the entry parameters
Call. addparameter ("OP1", xmltype. xsd_double, parametermode. In );
Call. setreturntype (xmltype. xsd_double );
D1 = new double (10.00 );
// Call the service
System. Out. println (d1 + "gallon equivalent to" +
Call. Invoke (new object [] {D1}) + "litre! ");
} // Main ()
}/* Axistest */
After compiling and running, you can see the following results:
190.0 Miles is equivalent to 305.71 kilometers!
10.0 gallon is equivalent to 45.46 litre!
Note that the program accesses the distance service that is instantly released and the custom capacity service. The service access point of the former is http: // localhost: 8080/axis/helloworld. JWS, while the latter is http: // localhost: 8080/axis/services/capacity.
VIII. service type: RPC, document, wrapped, and message
In axis, there are four service types:
1. RPC service:
The PRC service is the default service in axis. When you pass the <service... provider = "Java: RPC"> or <service... style = "RPC"> when the label is deployed, the RPC service is used. The RPC service complies with the soap RPC and Its Encoding specifications. Axis can deserialize XML into a Java object and pass it to the service method. In addition, Java objects returned by service methods can be serialized into XML.
2. Document/Wrapped Services
Document Services and wrapped services are similar in that neither uses the soap encoding for data; it's just plain old XML schema. in both cases, however, axis still "binds" Java representations to the XML (see the databinding section for more), so you end up dealing with Java objects, not directly with XML constructs.
Document and wrapped services do not use soap to encode data, which is similar to each other. They only use the old XML schema. However, in these two services, axis binds the representation of Java to the XML document, so you finally process Java objects instead of XML directly.
Below is a simple SOAP message containing an order. You can see the difference between the document and Wrapped Services:
<Soap: envelope xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<Soap: Body>
<Myns: purchaseorder xmlns: myns = "http://commerce.com/PO">
<Item> sk001 </item>
<Quantity> 1 </quantity>
<Description> sushi knife </description>
</Myns: purchaseorder>
</Soap: Body>
</Soap: envelope>
The XML format is as follows:
<Schema targetnamespace = "http://commerce.com/PO">
<Complextype name = "potype">
<Sequence>
<Element name = "item" type = "XSD: string"/>
<Element name = "quantity" type = "XSD: int"/>
<Element name = "Description" type = "XSD: string"/>
</Sequence>
</Complextype>
<Element name = "purchaseorder" type = "potype"/>
</Deployment>
For the document service, it maps to the following method:
Public void method (purchaseorder Po)
8. Soap envolop (SOAP envelope)
1. SOAP envelope
When the client sends a service request and the server returns the request result, the SOAP envelope is transmitted over the network. First, the client program sends the Request Parameters and request methods to the XML file (SOAP envelope) and transmits the SOAP envelope to the server. After receiving the SOAP envelope, the server parses the SOAP envelope, deserializes the call parameters and methods, and encapsulates the call results of the method into a SOAP envelope (sequential to the XML file) return to the client. The client deserializes the returned results encapsulated in the SOAP envelope to the desired results.
Let's take a look at the SOAP envelope of the following client program:
(1) Client Program:
Import org. Apache. axis. Client. call;
Import org. Apache. axis. Client. Service;
Import javax. xml. namespace. QNAME;
Public class testclient
{
Public static void main (string [] ARGs)
{
Try
{
String endpoint = "http://nagoya.apache.org: 5049/axis/services/echo ";
Service = new service ();
Call call = (CALL) service. createcall ();
Call. settargetendpointaddress (New java.net. URL (endpoint ));
Call. setoperationname (New QNAME ("http://soapinterop.org/", "echostring "));
String ret = (string) Call. Invoke (new object [] {"Hello! "});
System. Out. println ("sent 'Hello! ', Got' "+ RET + "'");
}
Catch (exception E)
{
System. Err. println (E. tostring ());
}
}
}
(2) SOAP envelope:
<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
SOAP-ENV: Body>
<NS1: echostring xmlns: NS1 = "http://soapinterop.org/">
<Arg0 xsi: TYPE = "XSD: string"> hello! </Arg0>
</NS1: echostring>
SOAP-ENV: Body>
SOAP-ENV: envelope>
2. Parameter Name:
In the code above, axis automatically names the function call parameters in the soap message as arg0, arg1, and so on. If you want to call the method according to your own defined parameter name, it is very easy, you only need to call the addparameter () function before calling the invoke function. As follows:
Call. addparameter ("testparam ",
Org. Apache. axis. constants. xsd_string,
Javax. xml. rpc. parametermode. In );
Call. setreturntype (Org. Apache. axis. constants. xsd_string );
Define testparam as the first parameter to call the function (there is only one parameter here). You can also define the type of this parameter and whether the parameter is input, output, or input/output. Here it is an input type. Now when you run the program, you will get the following message:
<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
SOAP-ENV: Body>
<NS1: echostring xmlns: NS1 = "http://soapinterop.org/">
<Testparam xsi: TYPE = "XSD: string"> hello! </Testparam>
</NS1: echostring>
SOAP-ENV: Body>
SOAP-ENV: envelope>
3. Return type
In the code above, we know that the echostring function will return a String object, and we also hope to return the expected String object through client calls. The following is a typical SOAP envelope (Message) obtained by calling the echostring function ).
<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
SOAP-ENV: Body>
<NS1: echostringresponse xmlns: NS1 = "http://soapinterop.org/">
<Result xsi: TYPE = "XSD: string"> hello! </Result>
</NS1: echostringresponse>
SOAP-ENV: Body>
SOAP-ENV: envelope>
We can see that the returned type (<result xsi: TYPE = "XSD: string">) is a string object. In this way, axis can deserialize the returned results into the string object we want.
Many tools put such definite type information into XML files to generate the "self-Describing part" of the message. On the other hand, some other tools return responses as follows: (define toolkits put this kind of explicit typing information in the XML to make the message "self-describing ". on the other hand, some toolkits return responses that look like this :)
<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
SOAP-ENV: Body>
<NS1: echostringresponse xmlns: NS1 = "http://soapinterop.org/">
<Result> hello, I'm a string! </Result>
</NS1: echostringresponse>
SOAP-ENV: Body>
SOAP-ENV: envelope>
There is no return type here, so how do we know what type of results should we deserialize the returned results? The answer is metadata. In this case, we need a description to specify the expected return type. The following code shows how to do this on the client:
Call. setreturntype (Org. Apache. axis. constants. xsd_string );
This method will tell the axis client that if the returned results do not specify the type, axis will specify the return type as the pre-defined soap type of the xsi: Type attribute, here, the xsd_string attribute specifies the string type.
Therefore, there is a similar method that allows you to specify the Java class to be returned.
Call. setreturnclass (string. Class );