2

私は castor 1.3.3-rc1 を使用していますが、この問題に戸惑っています。マニュアルを数回読んだことがあり、ここですべてを行ったと思いますが、取得し続けます:

java.lang.IllegalArgumentException: object is not an instance of declaring class{File: [not available]; line: 4; column: 43}

私のxmlをアンマーシャリングするとき。

これらは私のJavaクラスです:

public class ReportConfiguration {
    private List<ColumnMapping> columnMappings;

    // getters and setters omitted
}

public class ColumnMapping {
    private int index;
    private String label;
    private String sumTotal;

    // getters and setters omitted
}

これは、上記の Java クラスにアンマーシャリングされる私の xml データ ファイルです。

<reportConfiguration>
    <columnMappings>
        <columnMapping index="0" label="Login"/>
        <columnMapping index="1" label="Group"/>
        <columnMapping index="2" label="Profit" sumTotal="yes"/>
    </columnMappings>
</reportConfiguration>

そして、これは私のキャスターマッピングファイルです

<mapping>
    <class name="my.company.ReportConfiguration">
        <map-to xml="reportConfiguration"/>
        <field name="columnMappings" collection="arraylist" type="my.company.ColumnMapping">
            <bind-xml name="columnMappings"/>
        </field>
    </class>

    <class name="my.company.ColumnMapping">
        <map-to xml="columnMapping"/>
        <field name="index" type="integer" required="true">
            <bind-xml name="index" node="attribute"/>
        </field>
        <field name="label" type="string" required="true">
            <bind-xml name="label" node="attribute"/>
        </field>
        <field name="sumTotal" type="string">
            <bind-xml name="sumTotal" node="attribute"/>
        </field>
    </class>
</mapping>

Spring OXM を使用し、アプリケーション コンテキストに org.springframework.oxm.castor.CastorMarshaller インスタンスを作成し、Unmarshaller インスタンスを依存関係として注入しました。アンマーシャリングするときは、次のようにします。

ReportConfiguration config = (ReportConfiguration) unmarshaller.unmarshall(new StreamSource(inputStream));

誰が私が間違っていたのか/他にどのようにこの問題をデバッグできるのかを見つけることができますか?

4

1 に答える 1

4

ああ、実際に私は答えを見つけました。container="false"キャスター マッピングの属性を指定する必要があります。

<field name="columnMappings" collection="arraylist" type="my.company.ColumnMapping" container="false">
        <bind-xml name="columnMappings"/>
</field>

キャスターの説明書には次のように書かれています。

container フィールドをコンテナとして扱うかどうかを示します。つまり、そのフィールドのみを保持し、それを含むクラス自体は保持しないようにします。この場合、container 属性を true に設定する必要があります (Castor XML でのみサポートされます)。

デフォルトは true だと思います。この場合、キャスターは、の中に含まれていない、<columnMapping>直下の複数のインスタンスを見つけたいと考えています。<reportConfiguration><columnMappings>

より役立つエラー メッセージが表示される可能性があります。

于 2013-02-14T01:44:33.267 に答える