1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java继承属性后内存 java - Jackson:继承和必需的属性 - 堆栈内存溢出

Java继承属性后内存 java - Jackson:继承和必需的属性 - 堆栈内存溢出

时间:2019-08-20 15:09:57

相关推荐

Java继承属性后内存 java - Jackson:继承和必需的属性 - 堆栈内存溢出

我目前正在尝试使用能够处理多态性的杰克逊(Jackson)实现反序列化器,也就是说,给定这两个类:

public abstract class Animal {

private String name;

private float weight;

@JsonCreator

protected Animal(@JsonProperty(value="name") String name, @JsonProperty(value="weight",required=true) int weight) {

this.name=name;

this.weight=weight;

}

}

public class Dog extends Animal {

private int barkVolume;

@JsonCreator

public Dog(String name,int weight, @JsonProperty(value="barkVolume",required=true) int barkVolume) {

super(name, weight);

this.barkVolume=barkVolume;

}

}

解串器应该能够从json字符串中推断并实例化适当的子类。

我使用了一个自定义的反序列化器模块,UniquePropertyPolymorphicDeserializer(来自/robinhowlett/ce45e575197060b8392d )。 该模块的配置如下:

UniquePropertyPolymorphicDeserializer deserializer =

new UniquePropertyPolymorphicDeserializer(Animal.class);

deserializer.register("barkVolume", Dog.class);

SimpleModule module = new SimpleModule("UniquePropertyPolymorphicDeserializer");

module.addDeserializer(Animal.class, deserializer);

mapper.registerModule(module);

该模块向用户询问Animal的每个子类的唯一属性。 因此,当解串器找到具有barkVolume属性的json字符串时,它知道应该实例化Dog。

但是,我对json属性的规范存在疑问,因为子类不能从父类中给定的属性继承。 在Dog类中,我必须再次指定“ name”和“ weight”是json属性,即使在Animal类中已经指定了这些属性:

public Dog(@JsonProperty(value="name") String name, @JsonProperty(value="weight",required=true) int weight, @JsonProperty(value="barkVolume",required=true) int barkVolume) {

super(name, weight);

this.barkVolume=barkVolume;

}

否则,解串器将产生错误:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `Animals.Dog`: Argument #0 has no property name, is not Injectable: can not use as Creator [constructor for Animals.Dog, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}]

at [Source: UNKNOWN; line: -1, column: -1]

这个解决方案对我来说并不令人满意:

每次我们要创建Animal的新子类时,都必须在此类中指定name和weight为json属性。

这很棘手,例如在Animal类中,weight属性被标记为必需,而在子类中,我们可以定义weight不是必需属性。

您是否知道一种从父类的属性“继承”的方法,以避免每次在子类中指定对应的json属性?

最好的祝福,

马蒂厄

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。