繁体   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