[英]Restlet/Jackson JSON wrapper for ArrayList<Profile>
我將Restlet庫用於WS服務器,並且由於某些問題,我最近從XStream / Jettison切換為Jackson的JSON序列化器/反序列化器。
第一個缺點是我的ArrayList <Profile>(以前是Jettison的Vector)在序列化時不包裝Profile列表,而JSON代替“ Profile:[{firstProfile},{secondProfile}]”看起來像: [{firstProfile},{secondProfile}]
我可以通過手動告訴客戶端哪種映射是正確的映射來克服此問題,但我更喜歡使用KVC方法。
我環顧四周,似乎這是一個已知問題: http : //wiki.fasterxml.com/JacksonPolymorphicDeserialization (5.1缺少有關序列化的類型信息)建議:
最簡單的方法應該是返回“ Profile []配置文件”數組,但似乎不起作用,然后再嘗試我重新檢查過的其他解決方案,似乎可以使用@XmlRootElement(name =“ Profile”)來包裝JSON根元素: http : //jira.codehaus.org/browse/JACKSON-163?focusedCommentId=213588&page=com.atlassian.jira.plugin.system.issuetabpanels : comment-tabpanel#comment-213588
因此,要在Jackson上使用JAXB批注,您需要配置objectMapper: http ://wiki.fasterxml.com/JacksonJAXBAnnotations
但是在restlet中,您需要重寫createObjectMapper才能傳遞自定義轉換器(請參閱: http ://restlet-discuss.1400322.n2.nabble.com/Set-custom-objectMapper-to-Jackson-Extension-td6287812.html和http://restlet-discuss.1400322.n2.nabble.com/Jackson-Mix-in-Annotations-td6211060.html#a6231831 )
這就是我現在正在嘗試的! 問題是有沒有更簡單的方法來實現這一目標?
謝謝!!
我的解決方案是用以下方法注釋Profile類:
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
public class Profile extends Element implements Serializable {
現在,json現在看起來像:
{"Profile":{ ... }}
返回類型是一個子類別列表:
public class ProfileList extends ArrayList<Profile>
{}
參見http://wiki.fasterxml.com/JacksonPolymorphicDeserialization 5.1
我認為從JAX-B似乎對如何處理列表有一些規則的意義上來說,您真正想要的並沒有真正可用。 在RESTeasy郵件列表上查看此轉換器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.