我正在尝试理解为什么我(可能)需要 @JsonTypeName
在子类上(像所有 'internet; sujests to put )如果没有它也能工作 ?
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "aType")
@JsonSubTypes(Array(
new Type(value = classOf[ModelA], name = "ModelA"),
new Type(value = classOf[ModelB], name = "ModelB")
))
class BaseModel(val modelName:String)
//@JsonTypeName("SomeModel") // Commented. Do I need this?
class ModelA(val a:String, val b:String, val c:String, commonData:String) extends BaseModel(commonData) {
def this() = this("default", "default", "default" ,"default")
}
//@JsonTypeName("SomeModel") // Commented. Do I need this?
class ModelB(val a:String, val b:String, val c:String, commonData:String) extends BaseModel(commonData) {
def this() = this("default", "default", "default" ,"default")
}
最佳答案
你不需要它们。
documentation @JsonSubTypes.Type
解释
Definition of a subtype, along with optional name. If name is missing, class of the type will be checked for JsonTypeName annotation; and if that is also missing or empty, a default name will be constructed by type id mechanism. Default name is usually based on class name.
你应该有一个
@JsonSubTypes(Array(
new Type(value = classOf[ModelA], name = "ModelA")
...
class ModelA
或
@JsonSubTypes(Array(
new Type(value = classOf[ModelA])
...
@JsonTypeName("ModelA")
class ModelA
关于java - jackson 为什么我需要在子类上进行 JsonTypeName 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978725/