Hitachi

Cosminexus V11 アプリケーションサーバ Webサービス開発ガイド


9.5.1 Webサービスクライアントの実装クラスを作成する

Webサービスを利用するWebサービスクライアントの実装クラスを作成します。

Webサービスに対して1回の呼び出しをする,ディスパッチベースのWebサービスクライアントcom.sample.client.TestClientの作成例を次に示します。

なお,ディスパッチベースのWebサービスクライアントでは,SOAPバインディングのバージョンを明示する必要があるため,例ではSOAP 1.1/HTTPバインディングを指定しています。SOAP 1.2の場合は,SOAPBinding.SOAP11HTTP_BINDINGの部分をSOAPBinding.SOAP12HTTP_BINDINGに読み替えてください。

package com.sample.client;
 
import java.io.File;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.ws.soap.SOAPBinding;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
 
public class TestClient {
    public static void main( String[] args ) {
        // サービス生成
        QName port = new QName( "http://sample.com", "UserInfoPort" );
        Service service = Service.create(
            new QName("http://sample.com", "UserInfoService"));
        String serviceURL = "http://webhost:8085/dispatch_provider/UserInfoService";
 
        // サービスにポートを追加
        service.addPort( port, SOAPBinding.SOAP11HTTP_BINDING, serviceURL );
 
        // Dispatchオブジェクト生成
        Dispatch<SOAPMessage> dispatch = service.createDispatch(
            port, SOAPMessage.class, Service.Mode.MESSAGE );
 
        // 要求メッセージ
        SOAPMessage request = null;
        
        try{
            // 要求メッセージの生成
            request = MessageFactory.newInstance().createMessage();
            SOAPBody reqSoapBody = request.getSOAPBody();
 
            // 社員番号を設定
            SOAPBodyElement requestRoot= soapBody.addBodyElement(
                new QName("http://sample.com", "number"));
            SOAPElement soapElement = requestRoot.addChildElement(
                new QName("http://sample.com", "value"));
            soapElement.addTextNode( "1234" );
 
            // 添付ファイル(顔写真)を設定
            String filePath = "C:\\attachment.jpg";
            FileDataSource fds = new FileDataSource(filePath);
            AttachmentPart apPart =
                request.createAttachmentPart(new DataHandler(fds));
            request.addAttachmentPart(apPart);
 
            // SOAPメッセージの送受信
            SOAPMessage response = dispatch.invoke( request );
 
            // 応答メッセージからデータを取得
            SOAPBody resSoapBody = response.getSOAPBody();
            SOAPBodyElement resRoot =
                (SOAPBodyElement)resSoapBody.getChildElements().next();
            Iterator iterator = resRoot.getChildElements();
            String result =
                
 
((SOAPElement)iterator.next()).getFirstChild().getNodeValue();
 
            // 登録確認メッセージの表示
            System.out.println( "[RESULT] " + result );
        } catch( SOAPException e ) {
            e.printStackTrace();
      }
   }
}

作成したTestClient.javaは,UTF-8形式でc:\temp\jaxws\works\dispatch_provider\client\src\com\sample\client\ディレクトリに保存します。