【Axis2】【Ant】Antで、WSDLファイルからソースの自動生成を行う ~クライアント編~

はじめに

 * 以下の関連記事でWSDLファイルからコマンドで自動生成したが、めんどいのでAntで生成できるようにする
クライアント
http://blogs.yahoo.co.jp/dk521123/32003685.html

サンプル

ファイル構成

SampleService(Project)
 +- lib
 +- src(src配下に自動生成したファイルが出力される)
 +- WebContent
 |   + META-INF
 |      + HelloWorld.wsdl
 + build.xml

build.xml

* 「!★!」部分は、以下の関連記事のように、AXIS2_HOMEが設定されている用
http://blogs.yahoo.co.jp/dk521123/31944955.html
* 実行するとsrc配下に自動生成したファイルが出力される
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="w2j.client" name="SampleService">
    <property environment="env"/>
    <!-- !★! AXIS2_HOMEが設定されている場合は下記を使用してもいい -->
    <!-- property name="axis2.home" value="${env.AXIS2_HOME}"/ -->
    <property name="axis2.home" value="C:\axis2-1.6.2"/>
    <property name="input.wsdl" value="${basedir}/WebContent/META-INF/HelloWorld.wsdl"/>
    <property name="output.path" value="${basedir}"/>
    <path id="axis2.classpath">
        <fileset dir="${axis2.home}">
            <include name="lib/*.jar"/>
        </fileset>
    </path>
    <!-- Client code output -->
    <target name="w2j.client" description="Client code output from WSDL">
        <echo message="Client code output from WSDL ${input.wsdl}"/>
        <java classname="org.apache.axis2.wsdl.WSDL2Java" dir="." fork="yes">
            <classpath refid="axis2.classpath"/>
            <arg line="-uri ${input.wsdl}"/>
            <arg line="-u"/>
            <arg line="-s"/> <!-- 非同期コードは不要 -->
            <arg line="-p com.sample.client"/>
            <arg line="-ns2p ">http://helloworld.webservice.moodykettle.com=com.sample.client"/>
            <arg line="-o ${output.path}"/>
        </java>
    </target>
</project>

HelloWorld.wsdl

* 今回、使用するWSDLファイル
https://code.google.com/p/java-sample-programs/source/browse/trunk/WebService/WebContent/wsdl/HelloWorld.wsdl?r=15
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://helloworld.webservice.moodykettle.com" xmlns:intf="http://helloworld.webservice.moodykettle.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="">http://helloworld.webservice.moodykettle.com">
  <wsdl:types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="">http://helloworld.webservice.moodykettle.com">
      <element name="sayHello">
        <complexType>
          <sequence>
            <element name="name" type="xsd:string" />
          </sequence>
        </complexType>
      </element>
      <element name="sayHelloResponse">
        <complexType>
          <sequence>
            <element name="sayHelloReturn" type="xsd:string" />
          </sequence>
        </complexType>
      </element>
    </schema>
  </wsdl:types>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="impl:sayHelloResponse" name="parameters" />
  </wsdl:message>
  <wsdl:message name="sayHelloRequest">
    <wsdl:part element="impl:sayHello" name="parameters" />
  </wsdl:message>
  <wsdl:portType name="HelloWorld">
    <wsdl:operation name="sayHello">
      <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
      <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="sayHello">
      <wsdlsoap:operation soapAction="" />
      <wsdl:input name="sayHelloRequest">
        <wsdlsoap:body use="literal" />
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <wsdlsoap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloWorldService">
    <wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">
      <wsdlsoap:address location="http://localhost:8080/WebService/services/HelloWorld" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>


関連記事

Axis2 の自動生成のコマンドオプション - WSDL2Java / Java2WSDL -

http://blogs.yahoo.co.jp/dk521123/33842530.html

WSDLから クライアントを作成する

http://blogs.yahoo.co.jp/dk521123/32003685.html

WSDLから Webサービスを作成する

http://blogs.yahoo.co.jp/dk521123/31984636.html

WSDLファイルから独自例外作成し、その例外をキャッチする

http://blogs.yahoo.co.jp/dk521123/34666754.html

SoapUI について ~SOAPに関するテストツール~

http://blogs.yahoo.co.jp/dk521123/32212829.html