【Axis2】【Eclipse】 Axis2 と Eclipse を使ったWebサービスでクラスをやり取りするには...


ポイント

 * Axis2で、Webサービスでクラスをやり取りするには、
   対象のクラスのプロパティをGetter/Setterで定義しておくこと

# publicのプロパティだとダメだった...

サンプル

SampleWebMain.java

import javax.jws.WebMethod;
import javax.jws.WebParam;
//import javax.jws.WebService;

import com.sample.entities.SampleEntity;

//JAX-WSを使用する場合
//@WebService(serviceName = "SampleWebMainServiceWithJaxWs")
public class SampleWebMain {

  @WebMethod
  public SampleEntity getSampleEntity(
      @WebParam(name = "name")
      String name) {
    return new SampleEntity(name);
  }
}

SampleEntity.java

package com.sample.entities;

import java.io.Serializable;

//import javax.xml.bind.annotation.XmlRootElement;

// 【1】JAX-WSを使用する場合、@XmlRootElement/@XmlElementが必要
//@XmlRootElement(name="SampleEntity")
public class SampleEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  private String name;

  // 【2】JAX-WSを使用する場合、デフォルト・コンストラクタが必要
  public SampleEntity() {
  }

  // ★【重要】Getter/Setterが必要(public String nameだとAxis2では認識されない)★
  public SampleEntity(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

出力結果

リクエス
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="">http://sample.com">
    <x:Header/>
    <x:Body>
        <sam:getSampleEntity>
            <sam:name>Ken</sam:name>
        </sam:getSampleEntity>
    </x:Body>
</x:Envelope>
レスポンス
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="">http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header></soapenv:Header>
    <soapenv:Body>
        <ns:getSampleEntityResponse xmlns:ns="">http://sample.com">
            <ns:return xmlns:ax21="http://entities.sample.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:SampleEntity">
                <ax21:name>Ken</ax21:name>
            </ns:return>
        </ns:getSampleEntityResponse>
    </soapenv:Body>
</soapenv:Envelope>


関連記事

Axis2Eclipse を使って、 新規にWebサービスを構築する

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

wsimportコマンドによる Web サービス・クライアントの作成

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