- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有类层次结构 A <- B <- C。我不希望类 A 知道 C,所以我打算使用 A.type="B"表示它是类 B,然后是 B.type2 ="C"表示它是 C 类。
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = B.class, name = "B")})
public abstract class A {
private final String type;
public A(String type) {
this.type = type;
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type2")
@JsonSubTypes({@JsonSubTypes.Type(value = C.class, name = "C")})
public class B extends A {
private final String type2;
private final String propertyB;
@JsonCreator
public B(@JsonProperty("type2") String type2,
@JsonProperty("propertyB") String propertyB) {
super("B");
this.type2 = type2;
this.propertyB = propertyB;
}
}
public class C extends B {
private final String propertyC;
@JsonCreator
public C(@JsonProperty("propertyB") String propertyB,
@JsonProperty("propertyC") String propertyC) {
super("C", propertyB);
this.propertyC = propertyC;
}
}
当我将 C 的 JSON 读取到 A 类时,实际的 Java 对象是 B 类而不是 C。
@Test
void whenReadCJsonToA_thenObjectIsInstanceOfC() throws JsonProcessingException {
String json = "{\n" +
" \"type\" : \"B\",\n" +
" \"type2\" : \"C\",\n" +
" \"propertyB\" : \"b\",\n" +
" \"propertyC\" : \"c\"\n" +
"}";
A obj = objectMapper.readValue(json, A.class);
assertTrue(obj instanceof B, "obj is not instance of B"); // pass
assertTrue(obj instanceof C, "obj is not instance of C"); // fail
}
使上述测试通过的一种方法是
writing custom deserializer ,但是如果类包含许多字段,则此解决方案很乏味。
最佳答案
根据 this Jackson issue ,仅一种类型鉴别器属性支持多级继承。在我的代码中,只保留属性 type
并删除属性 type2
.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = B.class, name = "B")})
public abstract class A {
private final String type;
public A(String type) {
this.type = type;
}
}
@JsonSubTypes({@JsonSubTypes.Type(value = C.class, name = "C")})
public class B extends A {
private final String propertyB;
@JsonCreator
public B(@JsonProperty("propertyB") String propertyB) {
super("B");
this.propertyB = propertyB;
}
}
public class C extends B {
private final String propertyC;
@JsonCreator
public C(@JsonProperty("propertyB") String propertyB,
@JsonProperty("propertyC") String propertyC) {
super(propertyB);
this.propertyC = propertyC;
}
}
查看完整代码
this commit .
关于java - 多级@JsonTypeInfo 和@JsonSubTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65432203/
有没有办法使用 jackson 注释@JsonSubTypes来调用与类型相同的类? 解释如下: 我创建了这个具有许多子类型的大师类 @JsonTypeInfo( use = JsonTypeInfo
当使用 Jackson Datamapper 反序列化我的 yaml-config 时,配置中用于决定要创建的子类型的值将被省略,并且不会保存在创建的 java 对象中。 这是我正在尝试读取的配置..
Jackson 框架提供了基于注解的方法来在序列化过程中发出类型信息。 我不想在我的父类(super class)(动物)中使用@JsonSubTypes 注释。 相反,我想告诉我的子类,即 Dog
我的 JSON 结构: { ... "type":"post", // Type could vary "items":[] // Array of items
我有类层次结构 A <- B <- C。我不希望类 A 知道 C,所以我打算使用 A.type="B"表示它是类 B,然后是 B.type2 ="C"表示它是 C 类。 @JsonTypeInfo(u
我这样反序列化 jsons: { "type":"a", "payload" : {...} } 有效负载类型取决于类型。我的类(class): public class Sth {
我能够序列化和反序列化抽象基类注释的类层次结构 @JsonTypeInfo( use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTy
我有一个涉及动态 @JsonSubTypes 贡献者的系统,我正在尝试弄清楚如何重构我的代码(下面的示例)以不使用 @JsonSubTypes 注释而是动态地或在运行时提供这些值。有谁知道这是否可能?
是否可以使用@JsonTypeInfo.As.EXTERNAL_PROPERTY而无需使用@JsonSubtypes指定子类型? 用例是我想要创建公共(public)基类,它具有在其他项目中声明的不同
我想创建一个配置文件,允许我定义不同的数据生成器,每个生成器都需要不同的配置。但是,它们都共享相同的方法 generateRow,因此这些类都可以实现一个接口(interface)。我使用的是 Jac
我遵循从 SDN3 到 SDN4 的迁移路径,并遇到了此处记录的相同问题:SDN4 - Neo4j OGM - Jackson 2 答案中引用了一个补丁,并提到它将应用于 neo4j-ogm 1.1.
我正在尝试转换一些使用 Jackson 的 @JsonSubTypes 注释来管理多态性的 Java 代码。 这里是有效的 Java 代码: @JsonTypeInfo( use = Json
我试图让 REST 端点在 POST 时创建 Widget 的子类型, 这是所有Widget的基类 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include
我已经研究了实现对象类的最佳方式,继承并包含在一个对象中作为一个列表。 链接 1:How to configure Jackson to deserialize named types with de
Jackson 中使用的 @JsonTypeInfo 和 @JsonSubTypes 注释是什么? public class Lion extends Animal { private String
对于 FasterXML 的 Jackon JSON 开发人员来说,这是一个常见问题。当我使用类似的 POJO 时,我遇到过这个问题: package net.package.dogs.retriev
我是一名优秀的程序员,十分优秀!