[英]Cannot parse list of elements from xml with Jackson
當我嘗試解析以下XML時,Jackson出現錯誤:
<root>
<aCollection>
<language xml:lang="en">
<entry id="1" value="foo"/>
<entry id="2" value="bar"/>
<entry id="3" value="blah"/>
</language>
</aCollection>
</root>
我使用的類是:
@Data
@JacksonXmlRootElement(localName = "root")
public class RootDTO {
@JacksonXmlProperty(localName = "aCollection")
private CollectionDTO collection;
}
@Data
public class CollectionDTO {
@JacksonXmlProperty(localName = "language")
LanguageDTO language;
}
@Data
public class LanguageDTO {
@JacksonXmlProperty(localName = "entry")
EntryDTO[] entries;
}
@Data
public class EntryDTO {
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(isAttribute = true)
private String value;
}
錯誤是: Can not instantiate value of type [simple type, class com.tinkerdesk.viewer.model.DTO.EntryDTO] from JSON String; no single-String constructor/factory method
Can not instantiate value of type [simple type, class com.tinkerdesk.viewer.model.DTO.EntryDTO] from JSON String; no single-String constructor/factory method
如果刪除數組括號,它可以工作,但當然只能解析第一個entry
元素。 我發現我應該在EntryDTO[] entries;
頂部使用@JacksonXmlElementWrapper(useWrapping = false)
EntryDTO[] entries;
因為元素沒有包裝。 但是,這對我來說並不適合,因為useWrapping
中沒有com.fasterxml.jackson.xml.annotate.JacksonXmlElementWrapper
。 我的傑克遜核心版本是2.9.6和xml-databind 0.6.2。 現在有更新的方法嗎?
好吧,沒關系,我犯了一個愚蠢的錯誤。 首先,Jackson Dataformat Xml的0.6.2版本應該使我感到懷疑。 似乎在某個時候他們移到了一個新的命名空間,而我在gradle中使用了舊的命名空間,可悲的是,它解析為最近在2011年更新過的Maven存儲庫。更新該命名空間使其解析為2.9.5,它具有useWrapping屬性。 進行此設置可以解決該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.