2013-02-14 49 views
2

我正在使用castor 1.3.3-rc1,並且我一直對此問題感到困惑。已經閱讀手冊幾次,我相信我所做的一切,在這裏,但我不斷收到:解組使用castor的對象列表給出java.lang.IllegalArgumentException:對象不是聲明類的實例

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 
} 

。這是將上述

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

解組到Java類我的XML數據文件,這是我的蓖麻映射文件

<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,在我的appli上創建了一個org.springframework.oxm.castor.CastorMarshaller實例並創建一個Unmarshaller實例作爲依賴關係。當解組我只是做這樣的事情:

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

誰能發現我做了什麼錯了/我還能怎麼調試這個問題?

回答

4

其實我找到了答案。我需要在蓖麻映射提供container="false"屬性:

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

這是蓖麻手冊說:

容器表示字段是否應該被視爲一個 容器,即只有它的領域應該是堅持,但不包含包含類本身的 。在這種情況下,應將容器屬性 設置爲true(僅在Castor XML中受支持)。

我覺得默認的是真實的 - 在這種情況下蓖麻希望直屬<reportConfiguration>找到<columnMapping>多個實例,而不是包含內部<columnMappings>

一個更有用的錯誤消息,可以提交。

+0

非常感謝:)這節省了我的一天:) – 2013-09-20 21:19:20

相關問題