【问题标题】:Restlet XStream/Jackson different JSON wrapperRestlet XStream/Jackson 不同的 JSON 包装器
【发布时间】:2012-01-17 15:40:03
【问题描述】:

我想从 Xstream 切换到 Jackson 来序列化/反序列化 Restlet 服务器中的对象。如果我将 Xstream 库用于:

@Get("json")
public Profile retrieve() {

    Profile prf = new Profile (...);

    ...

    return prf;
}

我会得到类似的东西:

{"Profile":{"id": 1, "name": "jack" ... }}

与杰克逊在一起时,我只得到:

{"id": 1, "name": "jack" ... }

如何获得与 Jackson 相同的 JSON?我的客户端中的 KVC 对象需要这个(否则我需要手动指定映射)

如果我返回一个 ArrayList 我也有同样的问题,jackson 在序列化时不包装 Profiles 列表,而不是 JSON

{Profile:[{firstProfile}, {secondProfile}]}

看起来像:

[{firstProfile}, {secondProfile}]

我也一直在尝试使用 Jackson 注释:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
public class MyProfileServerResource extends ServerResource {

但它似乎没有被restlet解释

【问题讨论】:

    标签: json annotations jackson restlet xstream


    【解决方案1】:

    注解的正确位置是在 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/JacksonPolymorphicDeserialization5.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多