简体   繁体   English

使用Castor解组对象列表会给出java.lang.IllegalArgumentException:对象不是声明类的实例

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

I'm using castor 1.3.3-rc1 and I've been puzzled with this problem. 我正在使用脚轮1.3.3-rc1,对此问题感到困惑。 Have read the manuals few times and I believe I've done everything right here, but I keep getting : 已经阅读了几次手册,我相信我已经在这里做了所有事情,但是我不断得到:

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

when unmarshalling my xml. 当解组我的xml时。

These are my java classes: 这些是我的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
}

This is my xml data file which will be unmarshalled into java classes above 这是我的xml数据文件,将在上面的Java类中解组

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

And this is my castor mapping file 这是我的脚轮映射文件

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

I used Spring OXM, created a org.springframework.oxm.castor.CastorMarshaller instance on my application context, and injected an Unmarshaller instance as dependency. 我使用Spring OXM,在我的应用程序上下文中创建了org.springframework.oxm.castor.CastorMarshaller实例,并注入了Unmarshaller实例作为依赖项。 When unmarshalling I just do something like this: 取消编组时,我只需要执行以下操作:

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

Can anyone spot what did I do wrong / how else I can debug this problem ? 谁能发现我做错了什么/我还能如何调试这个问题?

Ah actually I found the answer. 啊,实际上我找到了答案。 I need to supply container="false" attribute on the castor mapping : 我需要在castor映射上提供container="false"属性:

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

This is what castor manual says: 这是脚轮手册说的:

container Indicates whether the field should be treated as a container, ie only it's fields should be persisted, but not the containing class itself. container指示是否应将字段视为容器,即仅应保留其字段,而不包含字段本身。 In this case, the container attribute should be set to true (supported in Castor XML only). 在这种情况下,容器属性应设置为true(仅在Castor XML中支持)。

I think the default is true -- in which case castor hopes to find multiple instance of <columnMapping> directly under <reportConfiguration> , not contained inside a <columnMappings> 我认为默认设置为true-在这种情况下,castor希望直接在<reportConfiguration>下找到<columnMapping>多个实例,而不包含在<columnMappings>

A more helpful error message could be presented. 可能会显示更有用的错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spark java:java.lang.IllegalArgumentException:对象不是声明类的实例 - spark java: java.lang.IllegalArgumentException: object is not an instance of declaring class java.lang.IllegalArgumentException:对象不是声明类的实例 - java.lang.IllegalArgumentException: object is not an instance of declaring class Hibernate:java.lang.IllegalArgumentException:object不是声明类的实例 - Hibernate : java.lang.IllegalArgumentException: object is not an instance of declaring class java.lang.IllegalArgumentException:对象不是声明类的实例吗? - java.lang.IllegalArgumentException: object is not an instance of declaring class? spring boot:java.lang.IllegalArgumentException:object不是声明类的实例 - spring boot : java.lang.IllegalArgumentException: object is not an instance of declaring class method.invoke-java.lang.RuntimeException:java.lang.IllegalArgumentException:对象不是声明类的实例 - method.invoke - java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class Grails 3.2.4拒绝保存域实例:java.lang.IllegalArgumentException:object不是声明类的实例 - Grails 3.2.4 refuses to save domain instance: java.lang.IllegalArgumentException: object is not an instance of declaring class exception java.lang.IllegalArgumentException:object不是在sun.reflect.NativeMethodAccessorImpl.invoke0声明类的实例 - exception java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.reflect.NativeMethodAccessorImpl.invoke0 java.lang.IllegalArgumentException:当我使用反射时,object不是声明类的实例 - java.lang.IllegalArgumentException: object is not an instance of declaring class when I use reflection 无法反映。 异常java.lang.IllegalArgumentException:object不是声明类的实例 - Unable to reflect. Exception java.lang.IllegalArgumentException: object is not an instance of declaring class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM