尝试解析简单类型的子类型时缺少类型 ID,缺少类型 ID 属性 类型

Missing type id when trying to resolve subtype of simple type, missing type id property type

我正在尝试为我的序列化编写一个 JUNIT class。我为 jackson 使用的版本是 jackson-databind-2.10.2 My Concrete class extends and abstract class .当我是 运行 我的 JUNIT 时出现异常,尝试解析 [simple type,ResshipInfo] 的子类型时缺少类型 ID:缺少类型 ID 属性 'type' 下面是我要连载的JSON。我错过了什么。请帮忙。

{
 "orderId" : "12345",
 "orderDocumentType" : "SALES"
}

JUNIT调用

InputStream is = this.getClass().getClassLoader().getResourceAsStream("samples/response.json");
String control = IOUtils.toString(is, Charsets.UTF_8);
ReshipInfo reshipInfo = objectMapper.readValue(control, ReshipInfo.class);

混凝土Class

 public class ReshipInfo extends AbstractRequest {
 private Integer returnGracePeriod;
 public ReshipInfo() {
 }
 public ReshipInfo(Builder builder) {
    super(builder.orderDocumentType);       
    returnGracePeriod = builder.returnGracePeriod;

 }
 }

摘要Class

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "type",
    visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = ReshipInfo.class, name = "SALES")
})
public abstract class AbstractRequest {
@JsonProperty(value = "type")
private OrderDocumentType orderDocumentType;
}

我在配置 Jackson 的 SetupContext 时使用抽象 class(在您的例子中是 AbstractRequest)。 像: context.setMixInAnnotations(ReshipInfo.class, AbstractRequest.class)

所以我的实体将 ReshipInfo 扩展为抽象,而 ReshipInfo 不扩展 AbstractRequest。