4.5.1 WSDLをインポートしない場合
WSDLをインポートしない場合のWSDLの例およびDIIのコーディング例を示します。なお,WSDLの例では次の情報が定義されています。
-
WSDLのサービス名
名前空間URI…http://example.com
ローカル名…StockQuoteService
-
WSDLのポート名
名前空間URI…http://example.com
ローカル名…StockQuote
-
WSDLのオペレーション名
名前空間URI…http://example.com
ローカル名…getLastTradePrice
-
オペレーションのパラメタ名…in0
(1) WSDLの例(StockQuote.wsdl)
<wsdl:definitions targetNamespace="http://example.com" xmlns:intf="http://example.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:message name="getLastTradePriceResponse"> <wsdl:part name="getLastTradePriceReturn" type="xsd:int"/> </wsdl:message> <wsdl:message name="getLastTradePriceRequest"> <wsdl:part name="in0" type="xsd:string"/> </wsdl:message> <wsdl:portType name="StockQuote"> <wsdl:operation name="getLastTradePrice" parameterOrder="in0"> <wsdl:input message="intf:getLastTradePriceRequest" name="getLastTradePriceRequest"/> <wsdl:output message="intf:getLastTradePriceResponse" name="getLastTradePriceResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="StockQuoteSoapBinding" type="intf:StockQuote"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getLastTradePrice"> <soap:operation soapAction=""/> <wsdl:input name="getLastTradePriceRequest"> <soap:body namespace="http://example.com" use="literal"/> </wsdl:input> <wsdl:output name="getLastTradePriceResponse"> <soap:body namespace="http://example.com" use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="StockQuoteService"> <wsdl:port binding="intf:StockQuoteSoapBinding" name="StockQuote"> <soap:address location="http://localhost:8080/StockQuote/services/StockQuote"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
(2) DIIのコーディング例
//SOAPクライアントの開始 ClientID cltID = Management.initializeClient(); // クライアント識別子と実行スレッドを関連づける Management.connectClientIDtoCurrentThread(cltID); try { // Serviceオブジェクトの生成 QName serviceName = new QName( "http://example.com", "StockQuoteService"); URL wsdlDocumentLocation = null; try { wsdlDocumentLocation = new URL("file:///C:/foo/StockQuote.wsdl"); } catch(MalFormedURLException e) { // 異常時の処理 } ServiceFactory factory = null; Service service = null; try { factory = ServiceFactory.newInstance(); service = factory.createService(wsdlDocumentLocation, serviceName) } catch(ServiceException e) { // 異常時の処理 } // Callオブジェクトの生成 QName portName = new QName( "http://example.com", "StockQuote"); QName operationName = new QName { "http://example.com", "getLastTradePrice"); javax.xml.rpc.Call call = null; try { call = service.createCall(portName, operationName); } catch(ServiceException e) { // 異常時の処理 } // サービス呼び出し // 入力パラメタを生成 Class inputClass = ((com.cosminexus.cws.xml.rpc.Call) call ).getParameterClassByName("in0"); Object in0 = null; in(inputClass == String.class) { in0 = "foo"; } Object inParams = new Object[] {in0}; Object ret = null; try { Object ret = call.invoke(inParams); } catch(RemoteException e) { // 異常時の処理 Throwable cause = e.getCause(); if(cause != null) { // 原因例外が存在する場合の処理 System.out.println(cause.getMessage()); } } //戻り値の処理 Class retClass = ret.getClass(); int price = 0; if(retClass == Integer.class) { price = ((Integer)ret).intValue(); } System.out.println("price : " + price); } finally { // クライアント識別子と実行スレッドの関連づけを解除する Management.disconnectClientIDtoCurrentThread(cltID); // SOAPクライアントの終了 Management.finalizeClient(cltID); }
例では,javax.xml.rpc.Callクラスとcom.cosminexus.cws.xml.rpc.Callクラスという二つのCallクラスが使われています。Javaでは,別パッケージに存在する,同じ名前のクラスをimport宣言に書けないため,同じクラス内でjavax.xml.rpc.Callクラスとcom.cosminexus.cws.xml.rpc.Callクラスを使用する場合には,例のように完全修飾名で指定する必要があります。