简体   繁体   English

Json Jersey反序列化:抽象类

[英]Json Jersey Deserialize : Abstract Class

I'm trying to deserialize some JSON with Jersey/Jackson in Java. 我试图用Java中的Jersey / Jackson反序列化一些JSON。

Here is an example of my JSON 这是我的JSON的一个例子

    {
        "text":"toto",
        "link":"titi",
        "items":[{
            "text":"toutou",
            "link":"tata",
            "items":[{
                "text":"toto2",
                "link":"toutou2",
                "data":"tonti",
            ]}
        ]}
    }

So what my Java model parts looks like this 那么我的Java模型部分就是这样的

public IItem {
    ...
}

public Item implements IItem {
    List<IItem> items;
    String text;
    String link;
    ...
}

public ItemData extends Item {
    String data;
    ...
}

Now, when i try to deserialize my JSON, object mapper doesnt know what concrete class to Use. 现在,当我尝试反序列化我的JSON时,对象映射器不知道要使用哪个具体类。 How do I tell him this ? 我怎么告诉他这个? The thing is that I want an Item (with a list of Item (with a list of ItemData)). 问题是我想要一个Item(带有Item列表(带有ItemData列表))。

I've though of only using one object containing all fields (text, link, data), but i'd prefer this type of design which appears better me. 我虽然只使用一个包含所有字段(文本,链接,数据)的对象,但我更喜欢这种类型的设计,它看起来更好我。 Do you think it's worth it ? 你认为这值得吗?

In reality I have more than one field that would replicate because JSON structure is a bit more complex, I've simplified it for sake of clarity. 实际上我有多个字段可以复制,因为JSON结构有点复杂,我为了清楚起见简化了它。

Question2 : After that i need to serialise my objects again in JSON (first part is only for temporary development, I'll be filling objects from a JDBC driver later on), how can I tell jersey to only display one level at the time ? 问题2 :之后我需要在JSON中再次序列化我的对象(第一部分仅用于临时开发,稍后我将从JDBC驱动程序填充对象),我怎么能告诉jersey当时只显示一个级别?

ie i request / I want the first level only, answer should be : 即我要求/我只想要第一级,答案应该是:

    {
        "text":"toto",
        "link":"titi",
        "items":[{
            "text":"toutou",
            "link":"tata",
        ]}
    }

and if i request /tata answer should be : 如果我要求/tata答案应该是:

    {
        "text":"toutou",
        "link":"tata",
        "items":[{
            "text":"toto2",
            "link":"toutou2",
            "data":"tonti"
        ]}
    }

(Question is more about how hiding the second level in my first request, I do understand how to handle the second request). (问题更多的是关于如何在我的第一个请求中隐藏第二级,我知道如何处理第二个请求)。

Thanks, 谢谢,

If your using Jackson, it has a feature that allows for proper deserialization in these kind of cases (polymorphic list of items). 如果你使用Jackson,它有一个功能,允许在这种情况下正确的反序列化(多态的项目列表)。 You can use the @JsonTypeInfo annotation to indicate you want the object type to be included in the JSON, which will then be used to deserialize the correct instances. 您可以使用@JsonTypeInfo批注指示您希望将对象类型包含在JSON中,然后将其用于反序列化正确的实例。 Here's an example: 这是一个例子:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
public IItem {
    // ...
}

This will add an attribute to each serialized representation of IItem, called @class, which Jackson will detect and use later on to deserialize the correct object instance. 这将为IItem的每个序列化表示添加一个属性,称为@class,Jackson将检测并稍后使用它来反序列化正确的对象实例。

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

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