使用 gradle 运行集成测试时出现异常。
我得到的例外是:
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of AbstractCommonDetails: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
Summary 类对我试图反序列化的细节有以下注释:
@Data
public class Summary {
@JsonTypeInfo(use = NAME, include = PROPERTY, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = CardDetails.class, name = "CARD")
})
private AbstractCommonDetails details;
}
AbstractCommonDetails 如下所示:
@Getter
@Setter
public abstract class AbstractCommonDetails {
private Long id;
public abstract String getPaymentMethod();
}
并且 CardDetails 具有注释和方法的@JsonTypeName("CARD")实现。@DatagetPaymentMethod
ObjectMapper 的配置和反序列化如下:
new ObjectMapper().findAndRegisterModules().readValue(IOUtils.toString(inputStream, "UTF-8"), Summary.class);
和示例 json:
{
"details":{
"@type":"CARD",
"amount":"10.00"
}
奇怪的是 - 将集成测试作为 Junit 测试运行成功,但是当使用gradlew test命令运行相同的集成测试时,它会失败,并出现前面提到的异常。可能是什么原因?
慕森卡
相关分类