0

Soap 処理には Apache Camel + JAXB を使用します。Java グラスは、cxf-codegen-plugin と呼ばれる Maven プラグインによって生成されます。

私が直面している問題は、リストであるプロパティを使用したいときです。その場合、正しいクラスのオブジェクトではなく、常に JAXBElement のリストを取得します。

この特定の xml が切り取られたとします。

<domainObjects avqxsi:type="avqsq:AssetAllocation" id="100" name="Some Name">
    <nodes>101</nodes>
    <nodes>102</nodes>
  </domainObjects>

現在、すべての「ノード」は、タイプ の異なるドメイン オブジェクトの ID ですAANode。したがって、xsd では次のように定義されます。

<xsd:complexType name="AssetAllocation">
    <xsd:complexContent>
      <xsd:extension base="avqsq:DomainObject">
        <xsd:sequence>
          <xsd:element ecore:reference="avqsq:AANode" maxOccurs="unbounded" name="nodes" type="xsd:IDREF"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

そして、いくつかの bindings.xml を定義しました:

<jaxb:bindings node="xsd:complexType[@name='AssetAllocation']//xsd:element[@name='nodes']">
            <jaxb:property>
                <jaxb:baseType name="my.api.xsd.AANode"/>
            </jaxb:property>
        </jaxb:bindings>

私が欲しいのは、次のような POJO プロパティです。

@XmlElementRef(name = "nodes")
protected List<AANode> nodes;

しかし、実行時に実際に得られるのはList<JAXBElement<AANode>>、ClassCastException につながる です。

編集 1:JAXBElement.class cxf-codegen フレームワークが、間違っていると思われる プロパティに注釈が付けられていることを明確に確認できるクラスを生成しているという事実を見逃しました。興味深いことに、手動でアノテーションを AANode.class に変更すると、IllegalAnnotationException: AANode" で失敗するか、そのサブクラスのいずれもこのコンテキストに認識されません。

public class AssetAllocation
    extends DomainObject
    implements Serializable, Equals, HashCode, ToString
{

    @XmlElementRef(name = "nodes", type = JAXBElement.class)
    protected List<AANode> nodes;
4

2 に答える 2

1

apache CXF コード生成プラグインは、要素の生成プロパティ フラグを設定するまで、常に JAXBElement を使用してコードを生成します。

Jaxb binding.xml を作成し、以下のように pom ファイルからコード生成プラグイン セクションでそのバインディング xml を参照してください。

binding.xml

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

コード生成プラグイン

<plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                        <configuration>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/META-INF/wsdl/CxfExampleService.wsdl</wsdl>
                                    <bindingFiles>
                                        <bindingFile>${basedir}/src/main/resources/META-INF/wsdl/binding/bindings.xml</bindingFile>
                                    </bindingFiles>                                 
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

これで問題は解決します

于 2015-08-26T14:17:48.367 に答える
0

実際、wsdl2java は間違った注釈を持つクラスを生成します。それ以外の

@XmlElementRef(name = "nodes", type = JAXBElement.class)
protected List<AANode> nodes;

次のものが期待されます。

@XmlIDREF
protected List<AANode> nodes;

これを bindings.xml で管理できませんでした。したがって、私の最終的な解決策は、バイトコード操作を使用して注釈を修正することです。そうすれば、生成されたクラスやジェネレータ自体をいじる必要がなくなります。

于 2015-09-08T15:18:21.960 に答える