MavenのAxis2プラグインを使ってWSDLと戯れてみる

RestfulなAPIが~ってのが世の中の主流となっているとはいえ、 SOAP/WSDLなんていうところをホゲホゲする必要が出て来ました。 ってことで、サーバー立ててクライアントから叩いてみる的なのをやってみたいと思います。   ■ 用語的な話 http://japan.internet.com/developer/20070717/27.html   によると、SOAPベースのWebサービスの構築方法は以下の2つがあるそうで。 1. ボトムアップ形式  最初にJavaクラスを構築しておき、そこからサービス記述子やWSDLを生成。 2. トップダウン形式  WSDLを構築しておき、そこからJavaクラスを生成。   1.でサーバーアプリとWSDLを作って、2.でWSDLからクライアントアプリを作る、 なんて流れでやっていけばイイ感じなのでしょうかね…。     Axis2クイックスタート   とりあえずAxis2のクイックスタートガイドがあったのでソレに沿って↓ http://axis.apache.org/axis2/java/core/docs/quickstartguide.html   って、更にググってったら、同じ会社の友達の力強いブログポストが。 Axis2 1.1.1 クイックスタートガイドその1: http://daipresents.com/2008/axis2_111_1/ Axis2 1.1.1 クイックスタートガイドその2: http://daipresents.com/2008/axis2_111_2/ さすが世界の@daipresentsやで。     Javaのクラスを作ってみる   とりあえず、こういう内容の無い、ホゲだかハゲだかみたいなクラス作って、

package com.shinodogg.sample.service.pojo;

import java.util.HashMap;
import java.util.Map;

public class HogeService {
    private Map hogeMap = new HashMap();

    public String getHoge(String hage) {
        return hogeMap.get(hage);
    }

    public void update(String hage, String hogeHage) {
        hogeMap.put(hage, hogeHage);
    }
}

    Mavenプラグインを入れてみる   ググっていくと、どうやら2つのAxis2Mavenプラグインがあるみたいです。 ・Java2WSDL(JavaからWSDLへ)WSDL2Code(WSDLからJavaへ)   今回は上記のようにJavaクラスは作ったので、Java2WSDLを入れてみます。 pom.xmlは↓のような感じで。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.shinodogg</groupId>
    <artifactId>WSDLsamle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2-java2wsdl-maven-plugin</artifactId>
        <configuration>
          <className>com.foo.myservice.MyHandler</className>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>java2wsdl</goal>
            </goals>
          </execution>
         </executions>
      </plugin>
    </plugins>
  </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
</project>

  mvn axis2-java2wsdl:java2wsdlって書いてあったので、 Eclipseのm2なアレでRunのGoalsにaxis2-java2wsdl:java2wsdlって入れて叩いたら、、 ダウンロードがはじまって、、

[INFO] ------------------------------------------------------------------------
[INFO] Building WSDLsamle 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- axis2-java2wsdl-maven-plugin:1.6.2:java2wsdl (default-cli) @ WSDLsamle ---
Downloading: http://repo.maven.apache.org/maven2/org/apache/axis2/axis2-java2wsdl/1.6.2/axis2-java2wsdl-1.6.2.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/axis2/axis2-java2wsdl/1.6.2/axis2-java2wsdl-1.6.2.pom (9 KB at 11.4 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/axis2/axis2-kernel/1.6.2/axis2-kernel-1.6.2.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/axis2/axis2-kernel/1.6.2/axis2-kernel-1.6.2.pom

  イイ感じかと思ったら、落ちちゃった。。

Downloaded: http://repo.maven.apache.org/maven2/org/apache/axis2/axis2-kernel/1.6.2/axis2-kernel-1.6.2.jar (1021 KB at 933.2 KB/sec)
java.lang.NullPointerException
    at sun.net.util.URLUtil.urlNoFragString(URLUtil.java:29)
    at sun.misc.URLClassPath.getLoader(URLClassPath.java:292)
    at sun.misc.URLClassPath.getResource(URLClassPath.java:168)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.apache.axis2.description.java2wsdl.Java2WSDLUtils.namespaceFromClassName(Java2WSDLUtils.java:65)
    at org.apache.axis2.description.java2wsdl.Java2WSDLUtils.schemaNamespaceFromClassName(Java2WSDLUtils.java:84)
    at org.apache.ws.java2wsdl.Java2WSDLBuilder.getSchemaTargetNamespace(Java2WSDLBuilder.java:120)
    at org.apache.ws.java2wsdl.Java2WSDLBuilder.generateWSDL(Java2WSDLBuilder.java:211)
    at org.apache.ws.java2wsdl.Java2WSDLCodegenEngine.generate(Java2WSDLCodegenEngine.java:60)
    at org.apache.axis2.maven2.java2wsdl.Java2WSDLMojo.execute(Java2WSDLMojo.java:335)

そんなクラスありがせんがな的な話で、もう一度pom.xmlを見直して↓で叩いてみたけどダメで、、

<configuration>
  <className>com.shinodogg.sample.service.pojo.HogeService</className>
</configuration>

ググったら↓のStackOverFlowに辿り着いて http://stackoverflow.com/questions/4839092/maven-axis2-java2wsdl-sun-net-util-urlutil-urlnofragstring-error mvn process-classes axis2-java2wsdl:java2wsdlでやってみろ、と。   ↓のような感じで、   MS932。。的なワーニングがありつつも、無事終わりました。

[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building WSDLsamle 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ WSDLsamle ---
[debug] execute contextualize
[WARNING] Using platform encoding (MS932 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ WSDLsamle ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- axis2-java2wsdl-maven-plugin:1.6.2:java2wsdl (default) @ WSDLsamle ---
[INFO]
[INFO] --- axis2-java2wsdl-maven-plugin:1.6.2:java2wsdl (default-cli) @ WSDLsamle ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.411s
[INFO] Finished at: Fri May 10 16:05:51 JST 2013
[INFO] Final Memory: 6M/122M
[INFO] ------------------------------------------------------------------------

  んで、Targetの中に出来たservice.wsdl見てみると、localhost:8080的な感じでWSDL出来てる~(超長いので端折りやす。。)

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://pojo.service.sample.shinodogg.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://pojo.service.sample.shinodogg.com">
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://pojo.service.sample.shinodogg.com">
            <xs:element name="update">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="hage" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="hogeHage" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getHoge">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="hage" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getHogeResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
~略~
    <wsdl:service name="HogeService">
        <wsdl:port name="HogeServiceHttpSoap11Endpoint" binding="ns:HogeServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/HogeService"/>
        </wsdl:port>
        <wsdl:port name="HogeServiceHttpSoap12Endpoint" binding="ns:HogeServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/HogeService"/>
        </wsdl:port>
        <wsdl:port name="HogeServiceHttpEndpoint" binding="ns:HogeServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/HogeService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

    ■ 別プロジェクトを作ってWSDLからクライアントとサーバーのスタブを作ってみる   上記で作ったWSDLを元に、呼び出す方をやってみます。   まずはMaven Project作って   Maven2 WSDL2Code Plug-in Guideに従ってやってみます。   pom.xmlを↓こんな感じにして

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-wsdl2code-maven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsdl2code</goal>
                    </goals>
                    <configuration>
                        <packageName>com.shinodogg.sample</packageName>
                        <wsdlFile>src/main/wsdl/myservice.wsdl</wsdlFile>
                        <databindingName>xmlbeans</databindingName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2</artifactId>
        <version>1.4</version>
    </dependency>
</dependencies>

  先ほど作ったWSDLファイルをmyserviceって名前で所定の場所に配置して(このGUI的なのナイスっすね)   叩いてみたら↓イロイロとブワーっと出ててきたので、 パッケージ org.apache.xmlbeans.impl.values は存在しません。 パッケージ org.apache.axiom.om は存在しません。 パッケージ org.apache.axiom.om.impl.llom は存在しません。 javax.wsdl.Definition にアクセスできません。

<dependencies>
    <dependency>
        <groupId>org.apache.axis2</groupId>
        <artifactId>axis2</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.geronimo.bundles</groupId>
        <artifactId>axiom-all</artifactId>
        <version>1.2.12_1</version>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

  そしたら今度は↓。。一筋縄にはいかないヤツよのぅ。。

The parameters 'packageName' for goal org.apache.axis2:axis2-wsdl2code-maven-plugin:1.4:wsdl2code are missing or invalid

  なんかググってくと↓のようにwsdl2codeがイケてないのであって、コードのジェネレートは出来るぜ、と。 https://issues.apache.org/jira/browse/AXIS2-3585   generate-sourcesをゴールにして叩くと、、   ガッツリ出来てますね~   コードみてくと、、ほほぅって感じで。

 

時間切れで、実際にサーバー立てて~ってとこまでイケなかったけど、 大体雰囲気掴めたので、あとは業務で実践するのみかのぅ。