簡體   English   中英

使用Castor解組對象列表會給出java.lang.IllegalArgumentException:對象不是聲明類的實例

[英]Unmarshalling list of objects using castor gives java.lang.IllegalArgumentException: object is not an instance of declaring class

我正在使用腳輪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
}

這是我的xml數據文件,將在上面的Java類中解組

<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));

誰能發現我做錯了什么/我還能如何調試這個問題?

啊,實際上我找到了答案。 我需要在castor映射上提供container="false"屬性:

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

這是腳輪手冊說的:

container指示是否應將字段視為容器,即僅應保留其字段,而不包含字段本身。 在這種情況下,容器屬性應設置為true(僅在Castor XML中支持)。

我認為默認設置為true-在這種情況下,castor希望直接在<reportConfiguration>下找到<columnMapping>多個實例,而不包含在<columnMappings>

可能會顯示更有用的錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM