6. Access to service
GET method service access
The general SOAP message is transmitted in a Post method, but can also be accessed through the GET. For example, a service given below - "HelloWorld", its source code is as follows:
File helloWorld.jwspublic class helloworld () {system.out.println ("Hello World!"); // Print Output Return "Hello World!"; // Return the corresponding string}}
This service gives an operation called "HelloWorld", returns a "Hello World!" And prints "Hello World!" At the server side, put the file "... / WebApps / Axis directory, you can directly access the service through the GET method, accessing the address http: // localhost: 8080 / axis / helloworld.jws? method = helloworld, you can see the return SOAP envelope message, at the same time The end gave the corresponding display information "Hello World!" Which indicates that the HelloWorld service has been successfully accessed. The generated SOAP envelope message is: XML Version = "1.0" encoding = "utf-8"?> -
7. Client service access programming
AXIS provides an API to implement SOAP, from http: // localhost: 8080 / axis / docs / apidocs / index.html, you can see Axis's API documentation.
Among them, org.apache.axis.client.call and org.apache.axis.client.Service are two commonly used classes. When the general client program wants to access a web service, you must generate a client's service object and Call object, first set the corresponding parameters to the Call object, including the location of the service, the location, operand, inlet parameters, return value type, etc., and finally call the invoke method access service of the Call object.
The following is given routines for accessing the web service --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, these objects are standard JAX-RPC objects, which are used to store data (Metadata).
Service service = new service (); call call = (call) service.createCall (); // Access to instant Distance services
// Set the access point Call.SetTargetendPointDress ("http: // localhost: 8080 / axis / distance.jws");
// Set the opera name Call.SetoperationName ("ConvertMile2kilometre);
// Set the entry parameter call.addparameter ("OP1", XMLTYPE.XSD_DOUBLE, ParameterMode.in);
// Set the return parameter type call.setReturntype (XMLTYPE.XSD_DOUBLE); Double D1 = New Double (190);
// Call service, incorporate in the invoke method is the array system.out.println containing call parameters (D1 "mile equivalent to" Call.invoke (New Object [] {D1}) "km!") ;
// Access Customized Capacity Service Call = (CALL) Service.createCall ();
// Set the access point Call.SetTargetendPointDress ("http: // localhost: 8080 / axis / service / capacity");
// Set the opera name Call.SetoperationName ("ConvertGallon2litre");
/ / Set the entry parameter call.addparameter ("OP1", XMLTYPE.XSD_DOUBLE, ParameterMode.in); call.setRetRntype; D1 = New Double (10.00);
// Call service system.out.println (D1 "gallon equivalent to" call.invoke (new object [] {d1} "liter!");
} // main ()
} / * Axistest * /
After the compilation is run, you can see the following results:
190.0 miles equivalent to 305.71 km!
10.0 gallons equivalent to 45.46 liters! Note that the program is different when accessing the instant Distance service and custom published Capacity service, the former's service access point address is http: // localhost: 8080 / axis / helloWorld.jws, while the latter is http: // localhost : 8080 / Axis / Services / Capacity.
8. Service Type: RPC, Document, Wrapped, And Message In Axis, there are four service types: 1. RPC service: PRC service is the default service in AXIS, when you pass
2.Document / Wrapped servicesDocument 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 encoded data, this is their similar place. They only use old XML patterns. However, in these two services, AXIS will bundle Java's representation into an XML document, so you finally processed Java objects without directly processing XML.
Below is a simple SOAP message containing orders, you can see the difference between Document and Wrapped services:
The corresponding XML mode is as follows:
8. SOAP Envolop (SOAP Envelope) 1. The SOAP envelope transmits a SOAP envelope in the network when the client issues a service request and the server returns the request. First of all, the client program will request parameters and request methods to XML files (SOAP envelopes) and transfer the SOAP envelope to the server. After receiving the SOAP envelope, the server side will resolve this SOAP envelope and retrore call parameters and methods and encapsulate the call results of the method (sequence into the XML file) to return to the client, the client also will SOAP The return result of the package in the envelope is reversed as the result of the desired result.
Let's look at the SOAP envelope of the 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 service = new service (); CALL Call = (call) Service.createCall (); Call.SettargetendPointDress (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"?>
2. Name the parameter: In the code above, AXIS automatically calls parameters in the SOAP message to name arg0, arg1, etc. If you want to call the method according to your own parameter name, it is very simple, in you call Invoke The addParameter () function can be called before the 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 call functions The first parameter (there is only one parameter here), which can also define the type of the parameter and the parameter is input, output or input the output type. 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"?>
Many tools will put this determined type information in the XML file to generate the "self-description section" of the message. On the other hand, there are some tools to return responses below: (Many Toolkits Put this Kind of Explicit) Typing information in the xml to make the message "self-describ". on the other hand 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>