杰克逊为什么我需要在子类上进行 JsonTypeName 注释

jackson why do I need JsonTypeName annotation on subclasses

this link

我正在尝试了解 为什么 我(可能)需要 @JsonTypeName 子类(就像所有“互联网”;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")
}

你不需要它们。

@JsonSubTypes.Typedocumentation说明

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