Apache axisでWebサービスを呼び出してみた
最近は通信系のアプリケーションを作成しているので、試しにWebサービスを呼び出してみました。
対象のWebサービス
WSDL : http://public.dejizo.jp/SoapServiceV11.asmx?WSDL サービスURL : http://public.dejizo.jp/SoapServiceV11.asmx
使用環境
Eclipse Kepler
Apache axis 1.4.0
Java 1.6(古い。。)
手順
Eclipseでjavaプロジェクトを作成
右クリック>Other>Web Services>Web Service Client>Next
Service definitionにWSDLのURLを入力して完了
ソースファイルが自動作成される
以下クラスを作成
例として、辞書の正式名称のリストを取得してみました。
import org.apache.axis.AxisFault; import jp.MyDictionary.SOAPServiceV11.DicInfo; import jp.MyDictionary.SOAPServiceV11.SOAPServiceV11Locator; import jp.MyDictionary.SOAPServiceV11.SOAPServiceV11Soap; public class Main { public static void main(String[] args) throws AxisFault { try { SOAPServiceV11Locator locator = new SOAPServiceV11Locator(); // エンドポイントURLを指定 locator.setSOAPServiceV11SoapEndpointAddress("http://public.dejizo.jp/SoapServiceV11.asmx"); SOAPServiceV11Soap service = locator.getSOAPServiceV11Soap(); DicInfo[] dicInfoArray = service.getDicList(""); for(DicInfo dicInfo : dicInfoArray){ System.out.println(dicInfo.getFullName()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
実行結果
Edict和英辞典 EJDict英和辞典 JMdict独和辞典 JMdict仏和辞典 JMdict露和辞典 JMdict和独辞典 JMdict和仏辞典 JMdict和露辞典 フリー百科辞典 ウィキペディア日本語版 三省堂 デイリーコンサイス英和辞典(試用版) 三省堂 デイリーコンサイス国語辞典(試用版) 三省堂 デイリーコンサイス和英辞典(試用版)
感想
WSDLから通信に必要なソースは自動生成されるので、こちらはクライアントコードを少し書くだけでWebサービスが利用できます。自分のWebサービスをマッシュアップしたい場合はどうぞ。
Webサービスのつくり方 ――「新しい」を生み出すための33のエッセイ Software Design plus
- 作者: 和田裕介
- 出版社/メーカー: 技術評論社
- 発売日: 2013/07/10
- メディア: Kindle版
- この商品を含むブログ (4件) を見る