Hello World SAAJの例

この例では、SAAJクライアントAPIを使用してサービス(または別のWebサービス)にアクセスする方法を示します。SAAJとは、SOAPメッセージを作成するための低レベルのインタフェースです。 この例では、一般的なHello Worldの例としてSAAJクライアントを開発します。

1 SAAJクライアント

次に、SOAP要求を作成し、Hello World Webサービスに送信するSAAJクライアントの例を示します。 これに応じてSOAP応答が抽出され、従来の「Hello World」メッセージが表示されます。
package saaj_simple;
                                                                           
import java.util.Iterator;
                                                                           
import javax.xml.soap.*;
import java.net.URL;
                                                                           
/**
   A simple SAAJ client that invokes the  Hello World  web service.
 */
                                                                           
public class Client
{
    public static void main(String[] args) throws Exception
    {
    |   // get the endpoint where the 'Hello World' web service is deployed
    |   URL service = new URL(args.length > 0 ? args[0] :
    |       "http://localhost:9090/hello");
    |                                                                      
    |   // set the MessageFactory System property
    |   System.setProperty("javax.xml.soap.MessageFactory",
    |       "com.sssw.jbroker.saaj.soap.MessageFactoryImpl");
    |                                                                      
    |   // set the ConnectionFactory System property
    |   System.setProperty("javax.xml.soap.SOAPConnectionFactory",
    |       "com.sssw.jbroker.saaj.soap.SOAPConnectionFactoryImpl");
    |                                                                      
    |   // construct a SOAP message
    |   MessageFactory factory = (MessageFactory) MessageFactory.newInstance();
    |   SOAPMessage message = factory.createMessage();
    |                                                                      
    |   // get the SOAP body
    |   SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
    |   SOAPBody body = envelope.getBody();
    |                                                                      
    |   // create the sayHello element
    |   Name name = envelope.createName("sayHello", "ns1", "http://www.hello");
    |   SOAPElement child = body.addBodyElement(name);
    |   child.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
    |                                                                      
    |   // prepare the message to be sent
    |   message.saveChanges();
    |                                                                      
    |   // post SOAP message to URL and get the response
    |   SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
    |   SOAPConnection connection = scf.createConnection();
    |   SOAPMessage reply = connection.call(message, service);
    |   connection.close();
    |                                                                      
    |   // extract and print response
    |   envelope = reply.getSOAPPart().getEnvelope();
    |   body = envelope.getBody();
    |   Iterator iter = body.getChildElements();
    |   SOAPElement response = (SOAPElement) iter.next();
    |   iter = response.getChildElements();
    |   SOAPElement result = (SOAPElement) iter.next();
    |   System.out.println(result.getValue());
    }
}
例で示されているように、SOAPメッセージングにSAAJ APIを使用すると、Novell exteNd WSSDKで生成されたスタブおよびスケルトンを使用する場合より長くなります。

Novell exteNd WSSDK tcptunnelツールを使用すると、渡される内容を確認することができます。 次に、HTTP接続からサーブレットコンテナに渡されるSOAPメッセージを示します。
?

<SOAP-ENV:Envelope
?xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
?<SOAP-ENV:Body>
? <ns1:sayHello xmlns:ns1='http://www.hello'
?? SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
?</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
図1: Hello Worldでクライアントからサーバに渡されるSOAPメッセージ

次に、サーバから返されるSOAP応答メッセージを示します。

<SOAP-ENV:Envelope
?xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'
?xmlns:xsd='http://www.w3.org/1999/XMLSchema'
?xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance'
?xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'>
?<SOAP-ENV:Body>
? <ns1:sayHelloResponse SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'
?? xmlns=''>
?? xmlns:ns1='http://www.hello'>
?? <result xsi:type='xsd:string'>Hello World!</result>
? </ns1:sayHelloResponse>
?</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
図2: Hello Worldでサーバからクライアントに返されるSOAPメッセージ
Copyright © 2000-2003, Novell, Inc.All rights reserved.