[英]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.