Java Jackson 多态:如何将多个子类型映射到同一个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18394819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:10:59  来源:igfitidea点击:

Hymanson polymorphism: How to map multiple subtypes to the same class

javajsonHymanson

提问by Yahya Cahyadi

I'm using Hymanson 1.9.x. Sticking with the Animals example, Here's what I'd like to do:

我正在使用Hyman逊 1.9.x。坚持动物的例子,这是我想做的:

Let's say I have an Animal class:

假设我有一个 Animal 类:

public class Animal {
    private String type;
    // accessors
}

public class Mammal extends Animal {
    private String diet;
    // accessors
}

public class Bird extends Animal {
    private boolean tropical;
    // accessors
}

I would like to be able to do something like this (where I map a few subtypes to one class, and a few more to a different class):

我希望能够做这样的事情(我将几个子类型映射到一个类,将更多的子类型映射到另一个类):

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Mammal.class, name = "Dog"),
                @JsonSubTypes.Type(value = Mammal.class, name = "Cat"),
                @JsonSubTypes.Type(value = Bird.class, name = "Dodo"},
                @JsonSubTypes.Type(value = Bird.class, name = "Cockatoo"})
public class Animal {

}

What I'm seeing right now is that Hymanson will only recognize the Dog-to-Mammal and the Dodo-to-Bird mapping. This is because StdSubtypeResolver._collectAndResolve() only allows the same class to get registered once (due to the implementation of NamedType.equals()).

我现在看到的是 Hymanson 只会识别 Dog-to-Mammal 和 Dodo-to-Bird 映射。这是因为 StdSubtypeResolver._collectAndResolve() 只允许同一个类注册一次(由于 NamedType.equals() 的实现)。

Is there a workaround to the issue I'm seeing?

我看到的问题有解决方法吗?

采纳答案by StaxMan

Perhaps not by using annotations. Problems comes from the fact that such mapping would not work for serialization, and existing mapping does expect one-to-one (bijection) relationship. But you may want to file an RFE at Hymanson-databind issue tracker; adding support may be possible.

也许不是通过使用注释。问题来自这样一个事实,即这种映射不适用于序列化,并且现有映射确实期望一对一(双射)关系。但是您可能想在Hymanson-databind 问题跟踪器上提交 RFE ;增加支持是可能的。

回答by hkasera

I also faced the same issue and found out that the Subtype mapping expects unique classes.

我也遇到了同样的问题,发现子类型映射需要唯一的类。

What I did was to create two classes that extend the same base class. The extended classes are empty as they have the same properties as base class. Then added them to the Subtype map. For example, in your case, it will be -

我所做的是创建两个扩展相同基类的类。扩展类是空的,因为它们与基类具有相同的属性。然后将它们添加到子类型映射中。例如,在您的情况下,它将是-

@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = Mammal.class, name = "Dog"),
            @JsonSubTypes.Type(value = Mammal.class, name = "Cat"),
            @JsonSubTypes.Type(value = BirdDodo.class, name = "Dodo"},
            @JsonSubTypes.Type(value = BirdCockatoo.class, name = "Cockatoo"})
public class Animal {

}

public class BirdCockatoo extends Cockatoo{}
public class BirdDodo extends Dodo{}

I understand it is the not the best approach but until the issue is not resolved, it could be the best way to fix this. I followed this approach for now.

我知道这不是最好的方法,但在问题未解决之前,它可能是解决此问题的最佳方法。我暂时遵循这种方法。

Hope it helps you!

希望对你有帮助!

回答by erkfel

The bug has been resolved in the version 2.6.0, so you just have to update Hymanson to version 2.6.0or later. The additional information is hereand here.

该错误已在2.6.0版中解决,因此您只需将 Hymanson 更新到2.6.0版或更高版本即可。附加信息在这里这里