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

时间:2020-05-06 23:32:35

标签: java spring jackson marshalling junit5

我正在尝试为我的序列化类编写一个JUNIT。我用于杰克逊的版本是jackson-databind-2.10.2我的Concrete类是extended和abstract class。当我运行我的JUNIT时,我遇到异常,尝试解析[简单类型,ReshipInfo]的子类型时缺少类型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);

具体课程

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

 }
 }

抽象类

@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;
}

1 个答案:

答案 0 :(得分:0)

我在配置Jackson的SetupContext时使用了一个抽象类(在您的情况下为-AbstractRequest)。 喜欢: context.setMixInAnnotations(ReshipInfo.class, AbstractRequest.class)

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