简体   繁体   English

用于ArrayList的Restlet / Jackson JSON包装器<Profile>

[英]Restlet/Jackson JSON wrapper for ArrayList<Profile>

i'm using the Restlet library for a WS server and i've recently switched from XStream/Jettison to Jackson as a JSON serializer/deserializer because of some issues. 我将Restlet库用于WS服务器,并且由于某些问题,我最近从XStream / Jettison切换为Jackson的JSON序列化器/反序列化器。

A first drawback is that my ArrayList< Profile > (previously a Vector with Jettison) it doesn't wrap the list of Profiles when serialized and the JSON instead of "Profile:[{firstProfile}, {secondProfile}]" it looks like: [{firstProfile}, {secondProfile}] 第一个缺点是我的ArrayList <Profile>(以前是Jettison的Vector)在序列化时不包装Profile列表,而JSON代替“ Profile:[{firstProfile},{secondProfile}]”看起来像: [{firstProfile},{secondProfile}]

I can overcome to this issue in the client telling manually which is the correct mapping but i would prefer to use a KVC approach. 我可以通过手动告诉客户端哪种映射是正确的映射来克服此问题,但我更喜欢使用KVC方法。

I've looked around and it seems that it's a known issue: http://wiki.fasterxml.com/JacksonPolymorphicDeserialization (5.1 Missing type information on Serialization) that it suggest to: 我环顾四周,似乎这是一个已知问题: http : //wiki.fasterxml.com/JacksonPolymorphicDeserialization (5.1缺少有关序列化的类型信息)建议:

  • Use arrays instead of Lists 使用数组而不是列表
  • Sub-class list, using class MyPojoList extends ArrayList { } 子类列表,使用类MyPojoList扩展了ArrayList {}
  • Force use of specific root type 强制使用特定的根类型

the simplest way it should be to return an "Profile[] profile" array but it seems not working, before trying the other solutions i've rechecked around and it seems that you can use a @XmlRootElement(name = "Profile") to wrap the JSON root element: http://jira.codehaus.org/browse/JACKSON-163?focusedCommentId=213588&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-213588 最简单的方法应该是返回“ 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

so for using JAXB annotations with Jackson you need to configure the objectMapper: http://wiki.fasterxml.com/JacksonJAXBAnnotations 因此,要在Jackson上使用JAXB批注,您需要配置objectMapper: http ://wiki.fasterxml.com/JacksonJAXBAnnotations

but in restlet to do so you need to override createObjectMapper to pass a Custom converter (see: http://restlet-discuss.1400322.n2.nabble.com/Set-custom-objectMapper-to-Jackson-Extension-td6287812.html and http://restlet-discuss.1400322.n2.nabble.com/Jackson-Mix-in-Annotations-td6211060.html#a6231831 ) 但是在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

this is what i'm trying now! 这就是我现在正在尝试的! the question is there a more straightforward way to achieve this?? 问题是有没有更简单的方法来实现这一目标?

Thanks!! 谢谢!!

the solution for me is to annotate the Profile class with: 我的解决方案是用以下方法注释Profile类:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
public class Profile extends Element implements Serializable {

and now the json now looks like: 现在,json现在看起来像:

{"Profile":{ ... }}

and the return type is a Sub-classed list: 返回类型是一个子类别列表:

public class ProfileList extends ArrayList<Profile>
{}

see http://wiki.fasterxml.com/JacksonPolymorphicDeserialization 5.1 参见http://wiki.fasterxml.com/JacksonPolymorphicDeserialization 5.1

I think what you want is not really available in a sense that JAX-B seems to have some rules on how to deal with lists. 我认为从JAX-B似乎对如何处理列表有一些规则的意义上来说,您真正想要的并没有真正可用。 See this converstation on the RESTeasy mailing list 在RESTeasy邮件列表上查看此转换器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM