【问题标题】:Missing type id when trying to resolve subtype of simple type, missing type id property type尝试解析简单类型的子类型时缺少类型 id,缺少类型 id 属性类型
【发布时间】:2020-05-06 23:32:35
【问题描述】:

我正在尝试为我的序列化类编写一个 JUNIT。我用于 jackson 的版本是 jackson-databind-2.10.2 我的具体类扩展和抽象类。当我运行 JUNIT 时出现异常,尝试解析 [simple type,ReshipInfo] 的子类型时缺少类型 id:缺少类型 id 属性“类型” 下面是我要序列化的 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;
}

【问题讨论】:

    标签: java spring jackson marshalling junit5


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 2020-04-05
      • 2022-12-23
      • 1970-01-01
      • 2021-04-20
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多