33.5.2 Webサービスクライアントの実装クラスを作成する
Webサービスを利用するWebサービスクライアントの実装クラスを作成します。
Webサービスに対して1回の呼び出しをするWebサービスクライアントの作成例を次に示します。
package com.sample.client;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.ws.soap.MTOMFeature;
import java.io.File;
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 {
//DataHandlerオブジェクト生成
File imageFile = new File("portrait.png");
if (!imageFile.exists()) {
throw new RuntimeException("Cannot find the file \"portrait.png\".");
}
FileDataSource fdSource = new FileDataSource(imageFile);
DataHandler dhandler = new DataHandler(fdSource);
UserInfoService service = new UserInfoService();
UserInfoImpl port = service.getUserInfoImplPort(new MTOMFeature());
UserData userdata = port.getUserData("1", dhandler);
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\streaming\client\src\com\sample\client\ディレクトリに保存します。