java - 使用 Castor 解码对象列表会给出 java.lang.IllegalArgumentException : object is not an instance of declaring class

标签 java xml unmarshalling castor

我使用的是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
}

这是我的 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 实例,并注入(inject)了一个 Unmarshaller 实例作为依赖项。解码时我只是做这样的事情:

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

任何人都可以发现我做错了什么/我还能如何调试这个问题?

最佳答案

啊,实际上我找到了答案。我需要提供container="false"脚轮映射上的属性:

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

脚轮手册是这样说的:

container Indicates whether the field should be treated as a container, i.e. only it's fields should be persisted, but not the containing class itself. In this case, the container attribute should be set to true (supported in Castor XML only).

我认为默认值是 true —— 在这种情况下,castor 希望找到 <columnMapping> 的多个实例。直属<reportConfiguration> ,不包含在 <columnMappings>

可以显示更有用的错误消息。

关于java - 使用 Castor 解码对象列表会给出 java.lang.IllegalArgumentException : object is not an instance of declaring class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866358/

相关文章:

java - 两个按钮导致相同的 Activity

java - 使用 Java 8 合并两个 map

Python lxml - 查找标签 block 修正

.net - 使用 SVN 时如何忽略 .NET 数据集定义 (XSD) 中属性顺序的变化?

java - Grails 和 Java : Proper way to unmarshall Json from Grails in Java app

java - Jaxb从XSD生成类时去除后缀 "Type"

java.lang.NoClassDefFoundError 尽管需要 jar

java - 如何在 Eclipse 和 Subversion 中开始一个新项目

java - Java中使用XPath检查XML中是否存在节点

java - 通过 JAXB 解码 XML 时缺少一些值