WebReference.com - Part 2 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (5/5)
[previous] |
Professional Java Web Services
Let's look at two Apache SOAP API's that are used in the application: Call
and
Response
. The Call
object is responsible for setting up the details of
the request:
// Build the SOAP RPC request message using the Call object
Call call = new Call();
call.setTargetObjectURI("urn:HelloWorldService");
call.setMethodName("getMessage");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
The above code snippet builds a SOAP request message that specifies that the RPC method call
request should be routed to the "HelloWorld"
service and that the
getMessage()
method should be invoked.
The Call.invoke()
method sends the request to the SOAP server. The following code
snippet depicts this:
// Create a URL object, which represents the endpoint
URL url = new URL(endPoint);
// Send the SOAP RPC request message using invoke() method
Response resp = call.invoke(url, "");
In the above code snippet a java.net.URL
object is constructed from the string
representation of the endpoint for the Apache SOAP runtime. If the user doesn't provide a different
endpoint as a parameter then the endpoint will be
https://localhost:8080/soap/servlet/rpcrouter
. The return value is a Response
object, which contains the response from the server.
The following code snippet checks the response received back from the server and displays a
SOAP Fault message if an error occurred. Otherwise, it displays the value returned by the
getMessage()
method:
// Check the response.
if (resp.generatedFault ()) { // Error Occured
Fault fault = resp.getFault ();
System.out.println ("The Following Error Occured: ");
System.out.println (" Fault Code = " + fault.getFaultCode ());
System.out.println (" Fault String = " + fault.getFaultString ());
} else { // Completed Successfully
Parameter result = resp.getReturnValue ();
System.out.println (result.getValue ());
}
The SOAP request message generated by the HelloWorldClient
application is below:
POST /soap/servlet/rpcrouter HTTP/1.0
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: 414
SOAPAction: ""
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi=https://www.w3.org/1999/XMLSchema-instance
xmlns:xsd="https://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:getMessage xmlns:ns1="urn:HelloWorldService"
SOAP-ENV:encodingStyle="https://schemas.xmlsoap.org/soap/encoding/">
</ns1:getMessage>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The SOAP response message sent back to the application is below:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: 485
Date: Wed, 19 Dec 2001 04:10:28 GMT
Server: Apache Tomcat/4.0.1 (HTTP/1.1 Connector)
Set-Cookie: JSESSIONID=D712520676C524504110A4C5D6E672E9;Path=/soap
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="https://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="https://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="https://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:getMessageResponse xmlns:ns1="urn:HelloWorldService"
SOAP-ENV:encodingStyle="https://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:string">Hello World!</return>
</ns1:getMessageResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
[previous] |
Created: May 23, 2002
Revised: May 23, 2002
URL: https://webreference.com/programming/java/webservices/chap3/2/5.html