SOAP をやってみた

JavaSOAPなるものをやってみた。
環境は JDK1.6, Ecplise3.4, Tomcat-6.0, Axis2 1.5.1 です。
まずSOAPとはいかなるものかを知るために、各所の記事を読みあさる。
http://ja.wikipedia.org/wiki/SOAP_%28%E3%83%97%E3%83%AD%E3%83%88%E3%82%B3%E3%83%AB%29 まぁ、こんな感じだ。

早速 Eclipse でプロジェクトを作成する・・・なんで作ればいいのだろう。
いろいろと調べた結果「動的 Web プロジェクト」で作成すればいいことがわかった。
http://goodjob.boy.jp/chirashinoura/id/129.html
がしかし、バージョンの違いのためかここに書いてある通りには動かない。
2のサーバアプリ作成のところが上手く行かないところで、最終的にプロジェクトのフォルダ構成を


axis2
 +src
 | +sample
 |   +axisversion
 |     +Version.java
 +META-INF
 | +services.xml
 +build
 +WebContent
   +axis2-web
   +META-INF
   +WEB-INF
とすることでうまく行った。

サーバ側のソースは


public class HelloWorld {
public String getHW() throws Exception {
return "Java++";
}
}
みたいな感じ。

で、まだうまく動かないところがある。
axis2 のバージョンが新しくて、対応されていないのだ。
axis2-1.4.1 にすると上手く行った。

クライアントはこんな感じで。


public class HelloWorldClient {
private static EndpointReference targetEPR =
new EndpointReference("http://localhost:8080/axis2/services/Hello");
public static void main(String[] args) {
try {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://localhost:8080/", "ns1");
OMElement payload = fac.createOMElement("getHW", ns);
Options options = new Options();
ServiceClient client = new ServiceClient();
options.setTo(targetEPR);
client.setOptions(options);
//Blocking invocation
OMElement result = client.sendReceive(payload);
System.out.println(result.toString());
}
catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}

なんかいろいろ大変だった。