我正在尝试使用Jackson来序列化具有多态性的实体。序列化的JSON字符串应该包括一个额外的"type"属性,其值为"groupA"或"groupB",但它没有出现。
我的实体看起来像这样:
@Entity
@Table(name = "\"group\"")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = GroupA.class, name = "groupA"),
@JsonSubTypes.Type(value = GroupB.class, name = "groupB")
})
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
public class Group implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
// ...
}
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
public class GroupA extends Group {
//...
}
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSerialize(include = JsonSerialize.Inclusion.ALWAYS)
public class GroupB extends Group {
// ...
}
您知道为什么序列化程序不添加 type 属性吗?