4

Axis2 Web サービスからの WSDL ファイルがあります。指定された WSDL ファイルを使用してクライアント スタブを生成するwsimportと、結果のクラスにJAXBElementパラメーターが必要になります。なぜそうなのですか?

生成されたクラスの 1 つからのサンプル メソッド:

JAXBElement<DataBean> value;

public void setValue(JAXBElement<DataBean> value)
{
    this.value = ((JAXBElement<DataBean>) value);
}

私はそれが次のようになることを期待しています(JAXBElementなし):

DataBean value;

public void setValue(DataBean value)
{
    this.value= (DataBean) value;
}

ネットで見たチュートリアルでは、クラスを JAXBElement に設定していません。何が問題なのですか?サーバーは Axis2 Web サービスであり、WSDL ファイルは Axis2 によって自動生成されることに注意してください。サーバーを制御できないという前提があります。

wsimportパラメータを JAXBElements に変換しないようにするにはどうすればよいですか?

以下は、WSDL ファイルからの抜粋です: (重要なタグのみを含めるために、いくつかのタグを無視しました)

<xs:element name="getData">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" name="getData" nillable="true" type="ax220:getData"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="getData">
    <xs:sequence>
        <xs:element minOccurs="0" name="value" nillable="true" type="ax219:DataBean"/>
    </xs:sequence>
</xs:complexType>

<wsdl:message name="getDataRequest">
    <wsdl:part name="parameters" element="ns:getData"/>
</wsdl:message>

<wsdl:message name="getDataResponse">
    <wsdl:part name="parameters" element="ns:getDataResponse"/>
</wsdl:message>

<wsdl:operation name="getData">
    <wsdl:input message="ns:getDataRequest" wsaw:Action="urn:getData"/>
    <wsdl:output message="ns:getDataResponse" wsaw:Action="urn:getDataResponse"/>
</wsdl:operation>

<wsdl:operation name="getData">
    <soap:operation soapAction="urn:getData" style="document"/>
    <wsdl:input>
        <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

<wsdl:operation name="getData">
    <soap12:operation soapAction="urn:getData" style="document"/>
    <wsdl:input>
        <soap12:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap12:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

<wsdl:operation name="getData">
    <http:operation location="getData"/>
    <wsdl:input>
        <mime:content type="text/xml" part="parameters"/>
    </wsdl:input>
    <wsdl:output>
        <mime:content type="text/xml" part="parameters"/>
    </wsdl:output>
</wsdl:operation>
4

2 に答える 2

6

このページで読んだように:

http://www.techdevtips.com/java/java-webservice-client-how-to-remove-jaxb要素

このコードでデータ バインディング ファイルを使用します。

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
  xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
  <jaxb:globalBindings generateElementProperty="false">
    <xjc:simple />
  </jaxb:globalBindings>
</jaxb:bindings>

バインディング属性(または実行可能ファイルを使用する場合は -b フラグ引数)を入力して、wsimport Ant タスクで使用します。

乾杯 :)

于 2014-05-27T16:03:13.460 に答える
2

はじめに:これはできないと思います。つまり、wsimport に別の方法でクラスを生成するように指示することはできないと思います。ただし、スキーマを別の方法で生成し、それでもサービスと通信できるように WSDL を変更する方法を説明できます。

WSDL から型定義を取得し、complexType の名前を調整して、上記で欠落していた DataBean の型を追加しました。それをスキーマに貼り付け、型定義からクラスを生成するために wsimport によって使用される JAXB スキーマ コンパイラである xjc でスキーマをコンパイルしました。スキーマは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/DataBean"
    xmlns:tns="http://www.example.org/DataBean" elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="getData">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" name="getDataType" type="tns:getDataType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="getDataType">
        <xs:sequence>
            <xs:element minOccurs="0" name="value" type="tns:DataBean" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="DataBean">
        <xs:simpleContent>
            <xs:extension base="xs:int" />
        </xs:simpleContent>
    </xs:complexType>
</schema>

コンパイラに特別なオプションは必要ありません。xjc を実行してスキーマ ファイルを指定するだけで、ソース ファイルがコンパイルされます。

JAXBElement生成されたクラスは、メソッド パラメーターとして使用しません。つまり、次のようになります。

protected DataBean value;

public DataBean getValue() {
    return value;
}

どうしてこれなの?要素定義から属性を削除したnillable="true"ところ、これでうまくいきました。nillable="true"ここでは、明示的な null 値が有効であると述べています。

<DataBean></DataBean> 

この属性を削除すると、サービスが実際にそこに null 値を書き込むと、コードで問題が発生します。しかし、すべての WSDL が生成された後、おそらく Axis2 は何らかの理由で nillable がそこにあるはずだと考えているだけかもしれませんが、実装では実際には使用されません。運が良ければ、そうではなく、WSDL を変更してもすべてが正常に機能します。

これが役立つことを願っています!もしそうでなければ、少なくとも私は今日何かを学びました;-)

于 2012-08-31T11:01:24.230 に答える