Hitachi

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


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

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

Webサービスに対して1回の呼び出しをするWebサービスクライアントの作成例を次に示します。

package com.sample.client;
 
import java.awt.Image;
import java.io.File;
 
import javax.imageio.ImageIO;
import javax.xml.ws.soap.MTOMFeature;
 
import com.sample.UserInfoImpl;
import com.sample.UserData;
import com.sample.UserInfoService;
import com.sample.UserInfoException_Exception;
 
public class TestClient {
 
    public static void main( String[] args ) {
        try {
            // Imageオブジェクト生成
            File imageFile = new File("portrait.png");
            if (!imageFile.exists()) {
                throw new RuntimeException("Cannot find the file \"portrait.png\".");
            }
            Image image = ImageIO.read(imageFile);
 
            UserInfoService service = new UserInfoService();
            UserInfoImpl port = service.getUserInfoImplPort(new MTOMFeature());
 
            UserData userdata = port.getUserData("1", image);
            System.out.print("[RESULT] " + userdata.getMessage());
            System.out.println(" Name:" + userdata.getName()
                              + ", Section:" + userdata.getSection());
        } catch(UserInfoException_Exception e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

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